Move the special directory query helpers into the places browser.

This commit is contained in:
Adam Reichold 2015-12-25 23:28:10 +01:00
parent e78d29ef35
commit b62b78e2f8
12 changed files with 146 additions and 232 deletions

View File

@ -20,23 +20,14 @@
*/
#include "safegtk.h"
#include "../rtgui/guiutils.h"
#include <glib/gstdio.h>
#include <fcntl.h>
#ifdef WIN32
#include <windows.h>
// for GCC32
#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif
#include <shlobj.h>
#include <Shlwapi.h>
#else
#include <cstdio>
#endif
#include "../rtgui/rtimage.h"
#include <memory>
#include <glib/gstdio.h>
#include "../rtgui/guiutils.h"
Glib::RefPtr<Gio::FileInfo> safe_query_file_info (Glib::RefPtr<Gio::File> &file)
{
@ -285,97 +276,3 @@ int safe_g_mkdir_with_parents(const Glib::ustring& dirName, int mode)
{
return ::g_mkdir_with_parents(dirName.c_str(), mode);
}
Glib::ustring safe_get_user_picture_dir()
{
#ifdef WIN32
// get_user_special_dir/pictures crashes on some Windows configurations.
// so we use the safe native functions here
WCHAR pathW[MAX_PATH] = {0};
if (SHGetSpecialFolderPathW(NULL, pathW, CSIDL_MYPICTURES, false)) {
char pathA[MAX_PATH];
WideCharToMultiByte(CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0);
return Glib::ustring(pathA);
} else {
return Glib::ustring("C:\\");
}
#else
return Glib::get_user_special_dir (G_USER_DIRECTORY_PICTURES);
#endif
}
Glib::ustring safe_get_user_home_dir()
{
#ifdef WIN32
// get_user_special_dir/pictures crashes on some Windows configurations.
// so we use the safe native functions here
WCHAR pathW[MAX_PATH] = {0};
if (SHGetSpecialFolderPathW(NULL, pathW, CSIDL_PERSONAL, false)) {
char pathA[MAX_PATH];
WideCharToMultiByte(CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0);
return Glib::ustring(pathA);
} else {
return Glib::ustring("C:\\");
}
#else
return Glib::get_home_dir();
#endif
}
#ifdef WIN32
Glib::ustring safe_get_user_profile_dir()
{
WCHAR pathW[MAX_PATH] = {0};
if (SHGetSpecialFolderPathW(NULL, pathW, CSIDL_PROFILE, false)) {
char pathA[MAX_PATH];
WideCharToMultiByte(CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0);
return Glib::ustring(pathA);
} else {
return Glib::ustring("C:\\");
}
}
#endif
Glib::ustring safe_get_user_desktop_dir()
{
#ifdef WIN32
// get_user_special_dir/pictures crashes on some Windows configurations.
// so we use the safe native functions here
WCHAR pathW[MAX_PATH] = {0};
if (SHGetSpecialFolderPathW(NULL, pathW, CSIDL_DESKTOP, false)) {
char pathA[MAX_PATH];
WideCharToMultiByte(CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0);
return Glib::ustring(pathA);
} else {
return Glib::ustring("C:\\");
}
#else
return Glib::get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
#endif
}
#ifdef WIN32
/*
* Test if the path is a root path based on the content of the string
*
* Warning: this function is a workaround for Windows platform, and not necessarily bullet proof
*/
bool safe_is_shortcut_dir (const Glib::ustring& path)
{
return PathIsRootA(path.c_str()) || safe_get_user_home_dir() == path || safe_get_user_desktop_dir() == path || safe_get_user_profile_dir() == path; // || safe_get_user_picture_dir() == path;
}
#endif

View File

@ -25,13 +25,4 @@ int safe_g_remove(const Glib::ustring& filename);
int safe_g_rename(const Glib::ustring& oldFilename, const Glib::ustring& newFilename);
int safe_g_mkdir_with_parents(const Glib::ustring& dirName, int mode);
Glib::ustring safe_get_user_picture_dir();
Glib::ustring safe_get_user_home_dir();
Glib::ustring safe_get_user_desktop_dir();
#ifdef WIN32
Glib::ustring safe_get_user_profile_dir();
bool safe_is_shortcut_dir (const Glib::ustring& filename);
#endif
#endif

View File

@ -18,18 +18,21 @@
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include "editorpanel.h"
#include "options.h"
#include "progressconnector.h"
#include "rtwindow.h"
#include "guiutils.h"
#include "procparamchangers.h"
#include <iostream>
#include "../rtengine/safegtk.h"
#include "../rtengine/imagesource.h"
#include "../rtengine/iccstore.h"
#include "soundman.h"
#include "rtimage.h"
#include <iostream>
#include "rtwindow.h"
#include "guiutils.h"
#include "popupbutton.h"
#include "options.h"
#include "progressconnector.h"
#include "procparamchangers.h"
#include "placesbrowser.h"
using namespace rtengine::procparams;
@ -1543,7 +1546,7 @@ void EditorPanel::saveAsPressed ()
if (safe_file_test (options.lastSaveAsPath, Glib::FILE_TEST_IS_DIR)) {
saveAsDialog = new SaveAsDialog (options.lastSaveAsPath);
} else {
saveAsDialog = new SaveAsDialog (safe_get_user_picture_dir());
saveAsDialog = new SaveAsDialog (PlacesBrowser::userPicturesDir ());
}
saveAsDialog->set_default_size (options.saveAsDialogWidth, options.saveAsDialogHeight);

View File

@ -17,22 +17,26 @@
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib/gstdio.h>
#include "filecatalog.h"
#include <iostream>
#include <iomanip>
#include "../rtengine/rt_math.h"
#include "filecatalog.h"
#include "filepanel.h"
#include <glib/gstdio.h>
#include "../rtengine/rt_math.h"
#include "../rtengine/safegtk.h"
#include "guiutils.h"
#include "options.h"
#include "rtimage.h"
#include "cachemanager.h"
#include "multilangmgr.h"
#include "guiutils.h"
#include "filepanel.h"
#include "renamedlg.h"
#include "thumbimageupdater.h"
#include "../rtengine/safegtk.h"
#include "batchqueue.h"
#include "rtimage.h"
#include "placesbrowser.h"
using namespace std;
@ -2027,10 +2031,9 @@ void FileCatalog::buttonBrowsePathPressed ()
FirstChar = BrowsePathValue.substr (0, 1);
if (FirstChar == "~") { // home directory
DecodedPathPrefix = Glib::get_home_dir();
DecodedPathPrefix = PlacesBrowser::userHomeDir ();
} else if (FirstChar == "!") { // user's pictures directory
//DecodedPathPrefix = g_get_user_special_dir(G_USER_DIRECTORY_PICTURES);
DecodedPathPrefix = safe_get_user_picture_dir();
DecodedPathPrefix = PlacesBrowser::userPicturesDir ();
}
if (!DecodedPathPrefix.empty()) {

View File

@ -1,4 +1,3 @@
/*
* This file is part of RawTherapee.
*
@ -18,9 +17,12 @@
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filepanel.h"
#include "rtwindow.h"
#include "../rtengine/safegtk.h"
#include "rtwindow.h"
#include "inspector.h"
#include "placesbrowser.h"
int FilePanelInitUI (void* data)
{
@ -181,7 +183,7 @@ void FilePanel::init ()
dirBrowser->open (argv1);
} else {
if (options.startupDir == STARTUPDIR_HOME) {
dirBrowser->open (safe_get_user_picture_dir());
dirBrowser->open (PlacesBrowser::userPicturesDir ());
} else if (options.startupDir == STARTUPDIR_CURRENT) {
dirBrowser->open (argv0);
} else if (options.startupDir == STARTUPDIR_CUSTOM || options.startupDir == STARTUPDIR_LAST) {
@ -189,7 +191,7 @@ void FilePanel::init ()
dirBrowser->open (options.startupPath);
} else {
// Fallback option if the path is empty or the folder doesn't exist
dirBrowser->open (safe_get_user_picture_dir());
dirBrowser->open (PlacesBrowser::userPicturesDir ());
}
}
}

View File

@ -392,28 +392,21 @@ void FlatField::flatFieldAutoSelectChanged()
}
void FlatField::setShortcutPath(Glib::ustring path)
void FlatField::setShortcutPath(const Glib::ustring& path)
{
if (path == "") {
if (path.empty ()) {
return;
}
#ifdef WIN32
try {
// Dirty workaround, waiting for a clean solution by using exceptions!
if (!safe_is_shortcut_dir(path))
#endif
{
if (lastShortcutPath != "") {
try {
flatFieldFile->remove_shortcut_folder(lastShortcutPath);
} catch (Glib::Error &err) {}
if (!lastShortcutPath.empty ()) {
flatFieldFile->remove_shortcut_folder (lastShortcutPath);
}
flatFieldFile->add_shortcut_folder (path);
lastShortcutPath = path;
try {
flatFieldFile->add_shortcut_folder(path);
} catch (Glib::Error &err) {}
}
} catch (Glib::Error&) {}
}

View File

@ -75,7 +75,7 @@ public:
void flatFieldFile_Reset ();
void flatFieldAutoSelectChanged ();
void flatFieldBlurTypeChanged ();
void setShortcutPath(Glib::ustring path);
void setShortcutPath (const Glib::ustring& path);
void setFFProvider (FFProvider* p)
{
ffp = p;

View File

@ -17,11 +17,17 @@
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include "placesbrowser.h"
#include "options.h"
#include "toolpanel.h"
#include "../rtengine/safegtk.h"
#ifdef WIN32
#include <windows.h>
#include <shlobj.h>
#include <Shlwapi.h>
#endif
#include "guiutils.h"
#include "rtimage.h"
#include "options.h"
#include "toolpanel.h"
PlacesBrowser::PlacesBrowser ()
{
@ -86,17 +92,14 @@ bool compareMountByRoot (Glib::RefPtr<Gio::Mount> a, Glib::RefPtr<Gio::Mount> b)
void PlacesBrowser::refreshPlacesList ()
{
placesModel->clear ();
// append home directory
Glib::RefPtr<Gio::File> hfile = Gio::File::create_for_path (safe_get_user_home_dir()); // Will send back "My documents" on Windows now, which has no restricted access
Glib::RefPtr<Gio::File> hfile = Gio::File::create_for_path (userHomeDir()); // Will send back "My documents" on Windows now, which has no restricted access
if (hfile && hfile->query_exists()) {
try {
Glib::RefPtr<Gio::FileInfo> info = safe_query_file_info (hfile);
if (info) {
if (auto info = hfile->query_info ()) {
Gtk::TreeModel::Row newrow = *(placesModel->append());
newrow[placesColumns.label] = info->get_display_name ();
newrow[placesColumns.icon] = info->get_icon ();
@ -104,19 +107,15 @@ void PlacesBrowser::refreshPlacesList ()
newrow[placesColumns.type] = 4;
newrow[placesColumns.rowSeparator] = false;
}
} catch (Gio::Error&) {
/* This will be thrown if the path doesn't exist */
}
} catch (Gio::Error&) {}
}
// append pictures directory
hfile = Gio::File::create_for_path (safe_get_user_picture_dir());
hfile = Gio::File::create_for_path (userPicturesDir());
if (hfile && hfile->query_exists()) {
try {
Glib::RefPtr<Gio::FileInfo> info = safe_query_file_info (hfile);
if (info) {
if (auto info = hfile->query_info ()) {
Gtk::TreeModel::Row newrow = *(placesModel->append());
newrow[placesColumns.label] = info->get_display_name ();
newrow[placesColumns.icon] = info->get_icon ();
@ -124,9 +123,7 @@ void PlacesBrowser::refreshPlacesList ()
newrow[placesColumns.type] = 4;
newrow[placesColumns.rowSeparator] = false;
}
} catch (Gio::Error&) {
/* This will be thrown if the path doesn't exist */
}
} catch (Gio::Error&) {}
}
if (!placesModel->children().empty()) {
@ -225,16 +222,16 @@ void PlacesBrowser::refreshPlacesList ()
Glib::RefPtr<Gio::File> hfile = Gio::File::create_for_path (options.favoriteDirs[i]);
if (hfile && hfile->query_exists()) {
Glib::RefPtr<Gio::FileInfo> info = safe_query_file_info (hfile);
if (info) {
Gtk::TreeModel::Row newrow = *(placesModel->append());
newrow[placesColumns.label] = info->get_display_name ();
newrow[placesColumns.icon] = info->get_icon ();
newrow[placesColumns.root] = hfile->get_parse_name ();
newrow[placesColumns.type] = 5;
newrow[placesColumns.rowSeparator] = false;
}
try {
if (auto info = hfile->query_info ()) {
Gtk::TreeModel::Row newrow = *(placesModel->append());
newrow[placesColumns.label] = info->get_display_name ();
newrow[placesColumns.icon] = info->get_icon ();
newrow[placesColumns.root] = hfile->get_parse_name ();
newrow[placesColumns.type] = 5;
newrow[placesColumns.rowSeparator] = false;
}
} catch(Gio::Error&) {}
}
}
}
@ -315,12 +312,12 @@ void PlacesBrowser::addPressed ()
Glib::RefPtr<Gio::File> hfile = Gio::File::create_for_path (lastSelectedDir);
if (hfile && hfile->query_exists()) {
Glib::RefPtr<Gio::FileInfo> info = safe_query_file_info (hfile);
if (info) {
options.favoriteDirs.push_back (hfile->get_parse_name ());
refreshPlacesList ();
}
try {
if (auto info = hfile->query_info ()) {
options.favoriteDirs.push_back (hfile->get_parse_name ());
refreshPlacesList ();
}
} catch(Gio::Error&) {}
}
}
@ -342,3 +339,52 @@ void PlacesBrowser::delPressed ()
refreshPlacesList ();
}
Glib::ustring PlacesBrowser::userHomeDir ()
{
#ifdef WIN32
// get_home_dir crashes on some Windows configurations,
// so we rather use the safe native functions here.
WCHAR pathW[MAX_PATH];
if (SHGetSpecialFolderPathW (NULL, pathW, CSIDL_PERSONAL, false)) {
char pathA[MAX_PATH];
if (WideCharToMultiByte (CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0)) {
return Glib::ustring (pathA);
}
}
return Glib::ustring ("C:\\");
#else
return Glib::get_home_dir ();
#endif
}
Glib::ustring PlacesBrowser::userPicturesDir ()
{
#ifdef WIN32
// get_user_special_dir crashes on some Windows configurations,
// so we rather use the safe native functions here.
WCHAR pathW[MAX_PATH];
if (SHGetSpecialFolderPathW (NULL, pathW, CSIDL_MYPICTURES, false)) {
char pathA[MAX_PATH];
if (WideCharToMultiByte (CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0)) {
return Glib::ustring (pathA);
}
}
return Glib::ustring ("C:\\");
#else
return Glib::get_user_special_dir (G_USER_DIRECTORY_PICTURES);
#endif
}

View File

@ -72,6 +72,12 @@ public:
void selectionChanged ();
void addPressed ();
void delPressed ();
public:
static Glib::ustring userHomeDir ();
static Glib::ustring userPicturesDir ();
};
inline void PlacesBrowser::setDirSelector (const PlacesBrowser::DirSelectionSlot& selectDir)

View File

@ -293,24 +293,14 @@ void ProfilePanel::save_clicked (GdkEventButton* event)
dialog.set_current_name (lastFilename);
//Add the user's default (or global if multiuser=false) profile path to the Shortcut list
#ifdef WIN32
// Dirty workaround, waiting for a clean solution by using exceptions!
if (!safe_is_shortcut_dir(options.getPreferredProfilePath()))
#endif
try {
dialog.add_shortcut_folder(options.getPreferredProfilePath());
} catch (Glib::Error &err) {}
try {
dialog.add_shortcut_folder(options.getPreferredProfilePath());
} catch (Glib::Error&) {}
//Add the image's path to the Shortcut list
#ifdef WIN32
// Dirty workaround, waiting for a clean solution by using exceptions!
if (!safe_is_shortcut_dir(imagePath))
#endif
try {
dialog.add_shortcut_folder(imagePath);
} catch (Glib::Error &err) {}
try {
dialog.add_shortcut_folder(imagePath);
} catch (Glib::Error&) {}
//Add response buttons the the dialog:
dialog.add_button(Gtk::StockID("gtk-cancel"), Gtk::RESPONSE_CANCEL);
@ -468,24 +458,14 @@ void ProfilePanel::load_clicked (GdkEventButton* event)
bindCurrentFolder (dialog, options.loadSaveProfilePath);
//Add the user's default (or global if multiuser=false) profile path to the Shortcut list
#ifdef WIN32
// Dirty workaround, waiting for a clean solution by using exceptions!
if (!safe_is_shortcut_dir(options.getPreferredProfilePath()))
#endif
try {
dialog.add_shortcut_folder(options.getPreferredProfilePath());
} catch (Glib::Error &err) {}
try {
dialog.add_shortcut_folder(options.getPreferredProfilePath());
} catch (Glib::Error&) {}
//Add the image's path to the Shortcut list
#ifdef WIN32
// Dirty workaround, waiting for a clean solution by using exceptions!
if (!safe_is_shortcut_dir(imagePath))
#endif
try {
dialog.add_shortcut_folder(imagePath);
} catch (Glib::Error &err) {}
try {
dialog.add_shortcut_folder(imagePath);
} catch (Glib::Error&) {}
//Add response buttons the the dialog:
dialog.add_button(Gtk::StockID("gtk-cancel"), Gtk::RESPONSE_CANCEL);

View File

@ -286,20 +286,13 @@ void SaveAsDialog::setInitialFileName (Glib::ustring fname)
fchooser->set_current_name(fname);
}
void SaveAsDialog::setImagePath (Glib::ustring ipath)
void SaveAsDialog::setImagePath (const Glib::ustring& imagePath)
{
const auto dirName = Glib::path_get_dirname (imagePath);
Glib::ustring path = Glib::path_get_dirname(ipath);
//Add the image's path to the Shortcut list
#ifdef WIN32
// Dirty workaround, waiting for a clean solution by using exceptions!
if (!safe_is_shortcut_dir(path))
#endif
try {
fchooser->add_shortcut_folder(path);
} catch (Glib::Error &err) {}
try {
fchooser->add_shortcut_folder (dirName);
} catch (Glib::Error&) {}
}

View File

@ -58,7 +58,7 @@ public:
int getSaveMethodNum ();
void setInitialFileName (Glib::ustring iname);
void setImagePath (Glib::ustring ipath);
void setImagePath (const Glib::ustring& imagePath);
void okPressed ();
void cancelPressed ();