]> source.dussan.org Git - tigervnc.git/commitdiff
Adjust default directory for configuration file save and load 1331/head
authorJohannes <bensky@arcor.de>
Sun, 12 Sep 2021 12:58:22 +0000 (14:58 +0200)
committerJohannes <bensky@arcor.de>
Tue, 21 Sep 2021 08:30:50 +0000 (10:30 +0200)
common/os/os.cxx
common/os/os.h
vncviewer/ServerDialog.cxx
vncviewer/ServerDialog.h

index 409a2ec662add17611fdc56640f1a7d01a95549a..aa9e2bae55375b6a2aab1c01326e8629fddfbc55 100644 (file)
@@ -36,7 +36,7 @@
 #include <shlobj.h>
 #endif
 
-int getvnchomedir(char **dirp)
+static int gethomedir(char **dirp, bool userDir)
 {
 #ifndef WIN32
        char *homedir, *dir;
@@ -68,19 +68,40 @@ int getvnchomedir(char **dirp)
                return -1;
 
        memcpy(dir, homedir, len);
-       memcpy(dir + len, "/.vnc/\0", 7);
+       if (userDir)
+               dir[len]='\0';
+       else
+               memcpy(dir + len, "/.vnc/\0", 7);
 #else
        dir = new TCHAR[MAX_PATH];
        if (dir == NULL)
                return -1;
 
-       ret = SHGetSpecialFolderPath(NULL, dir, CSIDL_APPDATA, FALSE);
+       if (userDir)
+               ret = SHGetSpecialFolderPath(NULL, dir, CSIDL_PROFILE, FALSE);
+       else
+               ret = SHGetSpecialFolderPath(NULL, dir, CSIDL_APPDATA, FALSE);
+
        if (ret == FALSE) {
                delete [] dir;
                return -1;
        }
-       memcpy(dir+strlen(dir), (TCHAR *)"\\vnc\\\0", 6);
+       if (userDir)
+               dir[strlen(dir)+1] = '\0';
+       else
+               memcpy(dir+strlen(dir), (TCHAR *)"\\vnc\\\0", 6);
 #endif
        *dirp = dir;
        return 0;
 }
+
+int getvnchomedir(char **dirp)
+{
+       return gethomedir(dirp, false);
+}
+
+int getuserhomedir(char **dirp)
+{
+       return gethomedir(dirp, true);
+}
+
index fcca20e1400f030aaaff84f9b6299a258744f0a9..6a8272f122fe5a2a4461e7c5d7507becff0e809e 100644 (file)
  */
 int getvnchomedir(char **dirp);
 
+/*
+ * Get user home directory.
+ * If HOME environment variable is set then it is used.
+ * Otherwise home directory is obtained via getpwuid function.
+ *
+ * Note for Windows:
+ * This functions returns array of TCHARs, not array of chars.
+ *
+ * Returns:
+ * 0 - Success
+ * -1 - Failure
+ */
+int getuserhomedir(char **dirp);
+
 #endif /* OS_OS_H */
index ba0386ca6aaf2afe9f25d2d553f066d36a24a576..569335913cfa57afc4df6ab69c9f9c309579e72a 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <errno.h>
 #include <algorithm>
+#include <libgen.h>
 
 #include <FL/Fl.H>
 #include <FL/Fl_Input.H>
@@ -67,6 +68,7 @@ ServerDialog::ServerDialog()
   y = margin;
   
   serverName = new Fl_Input_Choice(x, y, w() - margin*2 - server_label_width, INPUT_HEIGHT, _("VNC server:"));
+  usedDir = NULL;
 
   int adjust = (w() - 20) / 4;
   int button_width = adjust - margin/2;
@@ -119,6 +121,8 @@ ServerDialog::ServerDialog()
 
 ServerDialog::~ServerDialog()
 {
+  if (usedDir) 
+    free(usedDir);
 }
 
 
@@ -164,8 +168,12 @@ void ServerDialog::handleOptions(Fl_Widget *widget, void *data)
 void ServerDialog::handleLoad(Fl_Widget *widget, void *data)
 {
   ServerDialog *dialog = (ServerDialog*)data;
-  Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", _("TigerVNC configuration (*.tigervnc)"), 
-                                                     0, _("Select a TigerVNC configuration file"));
+
+  if (!dialog->usedDir)
+    getuserhomedir(&(dialog->usedDir));
+
+  Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"), 
+                                                      0, _("Select a TigerVNC configuration file"));
   file_chooser->preview(0);
   file_chooser->previewButton->hide();
   file_chooser->show();
@@ -181,6 +189,7 @@ void ServerDialog::handleLoad(Fl_Widget *widget, void *data)
   }
   
   const char* filename = file_chooser->value();
+  dialog->updateUsedDir(filename);
 
   try {
     dialog->serverName->value(loadViewerParameters(filename));
@@ -199,9 +208,11 @@ void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
   ServerDialog *dialog = (ServerDialog*)data;
   const char* servername = dialog->serverName->value();
   const char* filename;
-
-  Fl_File_Chooser* file_chooser = new Fl_File_Chooser("", _("TigerVNC configuration (*.tigervnc)"), 
-                                                     2, _("Save the TigerVNC configuration to file"));
+  if (!dialog->usedDir)
+    getuserhomedir(&dialog->usedDir);
+  
+  Fl_File_Chooser* file_chooser = new Fl_File_Chooser(dialog->usedDir, _("TigerVNC configuration (*.tigervnc)"), 
+                                                      2, _("Save the TigerVNC configuration to file"));
   
   file_chooser->preview(0);
   file_chooser->previewButton->hide();
@@ -220,6 +231,7 @@ void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
     }
     
     filename = file_chooser->value();
+    dialog->updateUsedDir(filename);
     
     FILE* f = fopen(filename, "r");
     if (f) {
@@ -227,12 +239,12 @@ void ServerDialog::handleSaveAs(Fl_Widget *widget, void *data)
       // The file already exists.
       fclose(f);
       int overwrite_choice = fl_choice(_("%s already exists. Do you want to overwrite?"), 
-                                      _("Overwrite"), _("No"), NULL, filename);
+                                       _("Overwrite"), _("No"), NULL, filename);
       if (overwrite_choice == 1) {
 
-       // If the user doesn't want to overwrite:
-       file_chooser->show();
-       continue;
+        // If the user doesn't want to overwrite:
+        file_chooser->show();
+        continue;
       }
     }
 
@@ -388,3 +400,10 @@ void ServerDialog::saveServerHistory()
 
   fclose(f);
 }
+
+void ServerDialog::updateUsedDir(const char* filename)
+{
+  char * name = strdup(filename);
+  usedDir = strdup(dirname(name));
+  free(name);
+}
index 8363977765080433044925081cdbcd81ef90a444..f9c3d136603eb6a2800951c66e060a8a6067d2f4 100644 (file)
@@ -45,10 +45,12 @@ protected:
 private:
   void loadServerHistory();
   void saveServerHistory();
+  void updateUsedDir(const char* filename);
 
 protected:
   Fl_Input_Choice *serverName;
   std::vector<std::string> serverHistory;
+  char *usedDir;
 };
 
 #endif