+ wxString filename;
+ // Look into .desktop files
+ bool cont = dir.GetFirst(&filename, _T("*.desktop"), wxDIR_FILES);
+ while(cont) {
+ wxFileName p(dirname, filename);
+ LoadKDEApp( p.GetFullPath() );
+ cont = dir.GetNext(&filename);
+ }
+ // Look recursively into subdirs
+ cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
+ while(cont) {
+ wxFileName p(dirname, wxEmptyString);
+ p.AppendDir(filename);
+ LoadKDEAppsFilesFromDir( p.GetPath() );
+ cont = dir.GetNext(&filename);
+ }
+}
+
+// Return base KDE directories.
+// 1) Environment variable $KDEHOME, or "~/.kde" if not set.
+// 2) List of directories in colon separated environment variable $KDEDIRS.
+// 3) Environment variable $KDEDIR in case $KDEDIRS is not set.
+// Notice at least the local kde directory is added to the list. If it is the
+// only one, use later the application 'kde-config' to get additional paths.
+static void GetKDEBaseDirs(wxArrayString& basedirs)
+{
+ wxString env = wxGetenv( wxT("KDEHOME") );
+ if(env.IsEmpty())
+ env = wxGetHomeDir() + wxT("/.kde");
+ basedirs.Add(env);
+
+ env = wxGetenv( wxT("KDEDIRS") );
+ if(env.IsEmpty()) {
+ env = wxGetenv( wxT("KDEDIR") );
+ if(! env.IsEmpty())
+ basedirs.Add(env);
+ } else {
+ wxStringTokenizer tokenizer(env, wxT(":"));
+ while(tokenizer.HasMoreTokens())
+ basedirs.Add( tokenizer.GetNextToken() );
+ }
+}
+
+static wxString ReadPathFromKDEConfig(const wxString& request)
+{
+ wxString str;
+ wxArrayString output;
+ if(wxExecute(wxT("kde-config --path ")+request, output) == 0 &&
+ output.Count() > 0)
+ str = output.Item(0);
+ return str;
+}
+
+// Try to find the "Theme" entry in the configuration file, provided it exists.
+static wxString GetKDEThemeInFile(const wxFileName& filename)
+{
+ wxString theme;
+ wxTextFile config;
+ if(filename.FileExists() && config.Open( filename.GetFullPath() )) {
+ size_t cnt = config.GetLineCount();
+ for(size_t i = 0; i < cnt; i++)
+ if(config[i].StartsWith(wxT("Theme="), &theme))
+ break;
+ }
+ return theme;
+}
+
+// Try to find a file "kdeglobals" in one of the directories and read the
+// "Theme" entry there.
+static wxString GetKDETheme(const wxArrayString& basedirs)
+{
+ wxString theme;
+ for(size_t i = 0; i < basedirs.Count(); i++) {
+ wxFileName filename(basedirs.Item(i), wxEmptyString);
+ filename.AppendDir( wxT("share") );
+ filename.AppendDir( wxT("config") );
+ filename.SetName( wxT("kdeglobals") );
+ theme = GetKDEThemeInFile(filename);
+ if(! theme.IsEmpty())
+ return theme;
+ }
+ // If $KDEDIRS and $KDEDIR were set, we try nothing more. Otherwise, we
+ // try to get the configuration file with 'kde-config'.
+ if(basedirs.Count() > 1)
+ return theme;
+ wxString paths = ReadPathFromKDEConfig(wxT("config"));
+ if(! paths.IsEmpty()) {
+ wxStringTokenizer tokenizer(paths, wxT(":"));
+ while( tokenizer.HasMoreTokens() ) {
+ wxFileName filename(tokenizer.GetNextToken(), wxT("kdeglobals"));
+ theme = GetKDEThemeInFile(filename);
+ if(! theme.IsEmpty())
+ return theme;
+ }
+ }
+ return theme;
+}
+
+// Get list of directories of icons.
+static void GetKDEIconDirs(const wxArrayString& basedirs,
+ wxArrayString& icondirs)
+{
+ wxString theme = GetKDETheme(basedirs);
+ if(theme.IsEmpty())
+ theme = wxT("default.kde");
+
+ for(size_t i = 0; i < basedirs.Count(); i++) {
+ wxFileName dirname(basedirs.Item(i), wxEmptyString);
+ dirname.AppendDir( wxT("share") );
+ dirname.AppendDir( wxT("icons") );
+ dirname.AppendDir(theme);
+ dirname.AppendDir( wxT("32x32") );
+ dirname.AppendDir( wxT("mimetypes") );
+ if( wxDir::Exists( dirname.GetPath() ) )
+ icondirs.Add( dirname.GetPath() );
+ }
+
+ // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
+ if(basedirs.Count() > 1)
+ return;
+ wxString paths = ReadPathFromKDEConfig(wxT("icon"));
+ if(! paths.IsEmpty()) {
+ wxStringTokenizer tokenizer(paths, wxT(":"));
+ while( tokenizer.HasMoreTokens() ) {
+ wxFileName dirname(tokenizer.GetNextToken(), wxEmptyString);
+ dirname.AppendDir(theme);
+ dirname.AppendDir( wxT("32x32") );
+ dirname.AppendDir( wxT("mimetypes") );
+ if(icondirs.Index(dirname.GetPath()) == wxNOT_FOUND &&
+ wxDir::Exists( dirname.GetPath() ) )
+ icondirs.Add( dirname.GetPath() );
+ }
+ }
+}
+
+// Get list of directories of mime types.
+static void GetKDEMimeDirs(const wxArrayString& basedirs,
+ wxArrayString& mimedirs)
+{
+ for(size_t i = 0; i < basedirs.Count(); i++) {
+ wxFileName dirname(basedirs.Item(i), wxEmptyString);
+ dirname.AppendDir( wxT("share") );
+ dirname.AppendDir( wxT("mimelnk") );
+ if( wxDir::Exists( dirname.GetPath() ) )
+ mimedirs.Add( dirname.GetPath() );
+ }
+
+ // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
+ if(basedirs.Count() > 1)
+ return;
+ wxString paths = ReadPathFromKDEConfig(wxT("mime"));
+ if(! paths.IsEmpty()) {
+ wxStringTokenizer tokenizer(paths, wxT(":"));
+ while( tokenizer.HasMoreTokens() ) {
+ wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
+ wxString dirname = p.GetPath(); // To remove possible trailing '/'
+ if(mimedirs.Index(dirname) == wxNOT_FOUND &&
+ wxDir::Exists(dirname) )
+ mimedirs.Add(dirname);
+ }