TDE base libraries and programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kxdglauncher.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* This file is part of the KDE project
  2. Copyright (C) 2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include <tqfile.h>
  17. #include <tqdir.h>
  18. #include <tqtimer.h>
  19. #include <tqstring.h>
  20. #include <tqtextcodec.h>
  21. #include <krun.h>
  22. #include <tdefile.h>
  23. #include <tdelocale.h>
  24. #include <tdeapplication.h>
  25. #include <tdecmdlineargs.h>
  26. #include <tdeglobalsettings.h>
  27. #include <kinputdialog.h>
  28. #include <tdemessagebox.h>
  29. #include <tdeconfig.h>
  30. #include <kurlrequester.h>
  31. #include <kurlrequesterdlg.h>
  32. #include <stdlib.h>
  33. #include "kxdglauncher.h"
  34. // helper function for reading xdg user dirs: it is required for obvious reasons
  35. void readXdgUserDirs(TQString *desktop, TQString *documents)
  36. {
  37. TQFile f( TQDir::homeDirPath() + "/.config/user-dirs.dirs" );
  38. if (!f.open(IO_ReadOnly))
  39. return;
  40. // set the codec for the current locale
  41. TQTextStream s(&f);
  42. s.setCodec( TQTextCodec::codecForLocale() );
  43. TQString line = s.readLine();
  44. while (!line.isNull())
  45. {
  46. if (line.startsWith("XDG_DESKTOP_DIR=")) {
  47. *desktop = TQString(line.remove("XDG_DESKTOP_DIR=").remove("\"")).replace("$HOME", TQDir::homeDirPath());
  48. }
  49. else if (line.startsWith("XDG_DOCUMENTS_DIR=")) {
  50. *documents = TQString(line.remove("XDG_DOCUMENTS_DIR=").remove("\"")).replace("$HOME", TQDir::homeDirPath());
  51. }
  52. line = s.readLine();
  53. }
  54. }
  55. TQString getDocumentPath()
  56. {
  57. TQString s_desktopPath;
  58. TQString s_documentPath;
  59. readXdgUserDirs(&s_desktopPath, &s_documentPath);
  60. if (s_documentPath.isEmpty() == true) {
  61. #ifdef Q_WS_WIN
  62. s_documentPath = getWin32ShellFoldersPath("Personal");
  63. #else
  64. s_documentPath = TQDir::homeDirPath() + "/Documents/";
  65. #endif
  66. }
  67. s_documentPath = TQDir::cleanDirPath( s_documentPath );
  68. if ( !s_documentPath.endsWith("/"))
  69. s_documentPath.append('/');
  70. return s_documentPath;
  71. }
  72. static TDECmdLineOptions options[] =
  73. {
  74. { "xdgname <argument>", I18N_NOOP("XDG variable name to open"), 0 },
  75. { "getpath", I18N_NOOP("Do not launch Konqueror; instead print path to directory if it exists)"), 0 },
  76. TDECmdLineLastOption
  77. };
  78. int main( int argc, char **argv)
  79. {
  80. TDECmdLineArgs::init( argc, argv, "kxdglauncher", I18N_NOOP("TDE XDG File Browser Launcher and Prompter"), I18N_NOOP("Prompts if directory does not exist, otherwise launches"), "1.0" );
  81. TDECmdLineArgs::addCmdLineOptions( options );
  82. TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
  83. TDEApplication app;
  84. app.disableSessionManagement();
  85. if (args->isSet( "xdgname" ) == true) {
  86. TQString desiredFolder = args->getOption("xdgname");
  87. if (desiredFolder == "DOCUMENTS") {
  88. TQDir myqdir;
  89. if (myqdir.exists(getDocumentPath(), TRUE) == true) {
  90. if (args->isSet( "getpath" ) == true) {
  91. printf("%s\n", (const char *)getDocumentPath().local8Bit());
  92. return 0;
  93. }
  94. else {
  95. KRun * run = new KRun( KURL(getDocumentPath()), 0, false, false );
  96. TQObject::connect( run, TQT_SIGNAL( finished() ), &app, TQT_SLOT( quit() ));
  97. TQObject::connect( run, TQT_SIGNAL( error() ), &app, TQT_SLOT( quit() ));
  98. app.exec();
  99. return 0;
  100. }
  101. }
  102. else {
  103. KURLRequesterDlg newDirectoryRequester(getDocumentPath(), i18n("Please confirm your Documents directory location<br>Upon confimation a new directory will be created"), 0, NULL, true);
  104. newDirectoryRequester.setCaption(i18n("Create Documents directory"));
  105. newDirectoryRequester.urlRequester()->setMode(KFile::Directory);
  106. if (newDirectoryRequester.exec() != TQDialog::Accepted) {
  107. return 1;
  108. }
  109. else {
  110. TQString newDirectory = newDirectoryRequester.urlRequester()->url();
  111. if (newDirectory == TQString::null) {
  112. return 1;
  113. }
  114. else {
  115. if (newDirectory.length() < 4096) {
  116. bool directoryOk = false;
  117. if (myqdir.exists(newDirectory, TRUE) == false) {
  118. if (myqdir.mkdir(newDirectory, TRUE) == true) {
  119. directoryOk = TRUE;
  120. }
  121. }
  122. else {
  123. directoryOk = TRUE;
  124. }
  125. if (directoryOk == true) {
  126. TQString xdgModifiedDirectory = newDirectory;
  127. xdgModifiedDirectory = xdgModifiedDirectory.replace(TQDir::homeDirPath(), "$HOME");
  128. while (xdgModifiedDirectory.endsWith("/")) {
  129. xdgModifiedDirectory.truncate(xdgModifiedDirectory.length()-1);
  130. }
  131. TDEConfig config(TQDir::homeDirPath() + "/.config/user-dirs.dirs", false, false);
  132. config.writeEntry("XDG_DOCUMENTS_DIR", TQString("\"") + xdgModifiedDirectory + TQString("\""), true);
  133. config.sync();
  134. if (args->isSet( "getpath" ) == true) {
  135. printf("%s\n", (const char *)getDocumentPath().local8Bit());
  136. return 0;
  137. }
  138. else {
  139. KRun * run = new KRun( getDocumentPath(), 0, false, false );
  140. TQObject::connect( run, TQT_SIGNAL( finished() ), &app, TQT_SLOT( quit() ));
  141. TQObject::connect( run, TQT_SIGNAL( error() ), &app, TQT_SLOT( quit() ));
  142. app.exec();
  143. }
  144. return 0;
  145. }
  146. else {
  147. KMessageBox::error(0, i18n("Unable to create directory ") + TQString("\"") + newDirectory + TQString("\"\n") + i18n("Please check folder permissions and try again"), i18n("Unable to create directory"));
  148. return 1;
  149. }
  150. }
  151. else {
  152. KMessageBox::error(0, i18n("Unable to create the directory ") + newDirectory + TQString("\n") + i18n("Directory path cannot be longer than 4096 characters"), i18n("Unable to create directory"));
  153. return 1;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. else {
  160. printf("[kxdglauncher] XDG variable not recognized\n");
  161. return 1;
  162. }
  163. }
  164. else {
  165. printf("[kxdglauncher] Please specify the desired XDG variable name with --xdgname\n");
  166. return 1;
  167. }
  168. }