diff options
Diffstat (limited to 'src/appimpl.cpp~')
-rw-r--r-- | src/appimpl.cpp~ | 700 |
1 files changed, 700 insertions, 0 deletions
diff --git a/src/appimpl.cpp~ b/src/appimpl.cpp~ new file mode 100644 index 0000000..b7f7f3f --- /dev/null +++ b/src/appimpl.cpp~ @@ -0,0 +1,700 @@ +/*************************************************************************** + * $Id: tork.cpp,v 1.160 2007/12/30 12:58:22 hoganrobert Exp $ + * Copyright (C) 2006 by Robert Hogan * + * robert@roberthogan.net * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +/* This file is part of the KDE project + Copyright (c) 2004 Kevin Ottens <ervin ipsquad net> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "appimpl.h" + +#include <kdebug.h> +#include <kglobalsettings.h> +#include <kstandarddirs.h> +#include <kdesktopfile.h> +#include <klocale.h> +#include <kiconloader.h> +#include <kservice.h> +#include <kservicegroup.h> + +#include <qapplication.h> +#include <qeventloop.h> +#include <qdir.h> + +#include <sys/stat.h> + + +AppImpl::AppImpl() : QObject() +{ +} + +bool AppImpl::listRoot(QValueList<KIO::UDSEntry> &list) +{ + + + kdDebug() << "AppImpl::listRoot" << endl; + + + QStringList dirList = QStringList::split(":",getenv("PATH")); + kdDebug() << dirList << endl; + QStringList names_found; +// QStringList dirList; +// dirList << "/usr/local/bin/"; +// dirList << "/usr/bin/"; +// dirList << "/usr/sbin/"; +// dirList << "/bin/"; + + + QStringList::ConstIterator dirpath = dirList.begin(); + QStringList::ConstIterator end = dirList.end(); + for(; dirpath!=end; ++dirpath) + { + QDir dir = *dirpath; + if (!dir.exists()) continue; + + QStringList filenames + = dir.entryList( QDir::Files | QDir::Readable ); + + KIO::UDSEntry entry; + + QStringList::ConstIterator filename = filenames.begin(); + QStringList::ConstIterator endf = filenames.end(); + + for(; filename!=endf; ++filename) + { + if (!names_found.contains(*filename)) + { + entry.clear(); + createEntry(entry, /**dirpath,*/ *filename); + if ( !entry.isEmpty() ) + { + list.append(entry); + names_found.append(*filename); + } + } + } + } + + kdDebug() << "AppImpl::listRoot" << endl; + + return true; +} + +bool AppImpl::parseURL(const KURL &url, QString &name, QString &path) const +{ + QString url_path = url.path(); + + int i = url_path.find('/', 1); + if (i > 0) + { + name = url_path.mid(1, i-1); + path = url_path.mid(i+1); + } + else + { + name = url_path.mid(1); + path = QString::null; + } + + return name != QString::null; +} + +bool AppImpl::realURL(const QString &name, const QString &path, + KURL &url) const +{ + url = findBaseURL(name); + if (!url.isValid()) + { + return false; + } + + url.addPath(path); + return true; +} + +bool AppImpl::statByName(const QString &filename, KIO::UDSEntry& entry) +{ + + + kdDebug() << "AppImpl::statByName" << endl; + + QStringList dirList; + dirList << "/usr/local/bin/"; + dirList << "/usr/bin/"; + + QStringList::ConstIterator dirpath = dirList.begin(); + QStringList::ConstIterator end = dirList.end(); + for(; dirpath!=end; ++dirpath) + { + QDir dir = *dirpath; + if (!dir.exists()) continue; + + QStringList filenames + = dir.entryList( QDir::Files | QDir::Readable ); + + + QStringList::ConstIterator name = filenames.begin(); + QStringList::ConstIterator endf = filenames.end(); + + for(; name!=endf; ++name) + { + if (*name==filename) + { + createEntry(entry, /**dirpath,*/ *name); + return true; + } + } + } + + return false; +} + +KURL AppImpl::findBaseURL(const QString &filename) const +{ + kdDebug() << "AppImpl::findBaseURL" << endl; + + QStringList dirList = KGlobal::dirs()->resourceDirs("system_entries"); + + QStringList::ConstIterator dirpath = dirList.begin(); + QStringList::ConstIterator end = dirList.end(); + for(; dirpath!=end; ++dirpath) + { + QDir dir = *dirpath; + if (!dir.exists()) continue; + + QStringList filenames + = dir.entryList( QDir::Files | QDir::Readable ); + + + KIO::UDSEntry entry; + + QStringList::ConstIterator name = filenames.begin(); + QStringList::ConstIterator endf = filenames.end(); + + for(; name!=endf; ++name) + { + if (*name==filename+".desktop") + { + KDesktopFile desktop(*dirpath+filename+".desktop", true); + if ( desktop.readURL().isEmpty() ) + { + KURL url; + url.setPath( desktop.readPath() ); + return url; + } + + return desktop.readURL(); + } + } + } + + return KURL(); +} + + +static void addAtom(KIO::UDSEntry &entry, unsigned int ID, long l, + const QString &s = QString::null) +{ + KIO::UDSAtom atom; + atom.m_uds = ID; + atom.m_long = l; + atom.m_str = s; + entry.append(atom); +} + + +void AppImpl::createTopLevelEntry(KIO::UDSEntry &entry) const +{ + entry.clear(); + addAtom(entry, KIO::UDS_NAME, 0, "."); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_ACCESS, 0555); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/system_directory"); + addAtom(entry, KIO::UDS_ICON_NAME, 0, "system"); +} + +void AppImpl::createEntry(KIO::UDSEntry &entry, +// const QString &directory, + const QString &file) +{ + kdDebug() << "AppImpl::createEntry" << endl; + + entry.clear(); + + addAtom(entry, KIO::UDS_NAME, 0, file); + + QString new_filename = file; + new_filename.truncate(file.length()-8); + addAtom(entry, KIO::UDS_URL, 0, "app:/"+new_filename); + + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/directory"); + + KService::Ptr service = KService::serviceByDesktopName( new_filename ); + if (service && service->isValid()) + addAtom(entry, KIO::UDS_ICON_NAME, 0, service->icon()); + else + addAtom(entry, KIO::UDS_ICON_NAME, 0, "binary"); + +} + +void AppImpl::createExeEntry(QValueList<KIO::UDSEntry> &list,KIO::UDSEntry &entry, + const QString &shortname, + const QStringList &fullname) +{ + + + for ( QStringList::ConstIterator it = fullname.begin(); it != fullname.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + entry.clear(); + +/* QStringList dirList = KGlobal::instance()->dirs()->resourceDirs("apps"); + kdDebug() << dirList << endl; + QStringList desktop = getFullLocation(dirList,shortname+".desktop", + QDir::FilterSpec(QDir::Files | QDir::Dirs | QDir::Readable), + false,true); + kdDebug() << desktop << endl;*/ + KService::Ptr service = KService::serviceByDesktopName( shortname ); + +// if (!desktop.isEmpty()){ + if (service && service->isValid()) { + +// for ( QStringList::Iterator dit = desktop.begin(); dit != desktop.end(); ++dit ) +// { +// if ((*dit).isEmpty()) +// continue; +// entry.clear(); + + addAtom(entry, KIO::UDS_NAME, 0, i18n("Application (%1)").arg(service->name())); + addAtom(entry, KIO::UDS_URL, 0, locate("apps", service->desktopEntryPath())); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFREG); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "application/x-desktop"); + addAtom(entry, KIO::UDS_ICON_NAME, 0, service->icon()); +// createFileEntry(entry, service->name(), url.url(1)+service->desktopEntryName(), +// "application/x-desktop", service->icon(), locate("apps", +// service->desktopEntryPath()) ); + + if ( !entry.isEmpty() ) + list.append(entry); +// } + return; + } + + addAtom(entry, KIO::UDS_NAME, 0, i18n("Application (%1)").arg(*it)); + addAtom(entry, KIO::UDS_URL, 0, *it); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFREG); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "application"); + addAtom(entry, KIO::UDS_ICON_NAME, 0, "binary"); + if ( !entry.isEmpty() ) + list.append(entry); + } + +} + +void AppImpl::createManPageEntry(KIO::UDSEntry &entry, + const QString &shortname) +{ + + entry.clear(); + + addAtom(entry, KIO::UDS_NAME, 0, i18n("Manual for %1").arg(shortname)); + +/* QString new_filename = file; + new_filename.truncate(file.length()-8);*/ + addAtom(entry, KIO::UDS_URL, 0, "man:/"+shortname); + + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFREG); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "application/x-desktop"); + + QString icon = "man"; + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); +} + +void AppImpl::createHomeDirEntry(QValueList<KIO::UDSEntry> &list,KIO::UDSEntry &entry, + const QString &shortname) +{ + + QStringList homedir; + homedir << QString("%1/").arg(getenv("HOME")); + + QString dirname = "."+shortname; + QStringList fullname = getFullLocation(homedir,dirname, + QDir::FilterSpec(QDir::Hidden | QDir::Dirs | QDir::Readable), + true, false); + + for ( QStringList::Iterator it = fullname.begin(); it != fullname.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + entry.clear(); + + addAtom(entry, KIO::UDS_NAME, 0, i18n("User Data (%1)").arg(*it)); + addAtom(entry, KIO::UDS_URL, 0, *it); + + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/directory"); + + QString icon = "cachefolder"; + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); + if ( !entry.isEmpty() ) + list.append(entry); + } +} + +void AppImpl::createKDEDataDirEntry(QValueList<KIO::UDSEntry> &list,KIO::UDSEntry &entry, + const QString &shortname) +{ + + QStringList dirList = KGlobal::instance()->dirs()->resourceDirs("data"); + + QStringList KDEDataDir = getFullLocation(dirList,shortname, + QDir::FilterSpec(QDir::Dirs | QDir::Readable), false, false); + if (KDEDataDir.isEmpty()) + return; + + + for ( QStringList::Iterator it = KDEDataDir.begin(); it != KDEDataDir.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + entry.clear(); + + addAtom(entry, KIO::UDS_NAME, 0, i18n("App Data (%1)").arg(*it)); + addAtom(entry, KIO::UDS_URL, 0, *it); + + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/directory"); + + QString icon = "binaryfolder"; + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); + if ( !entry.isEmpty() ) + list.append(entry); + } + +} + +void AppImpl::createStandardDataDirEntry(QValueList<KIO::UDSEntry> &list, KIO::UDSEntry &entry, + const QString &shortname) +{ + + + QStringList dirList; + dirList << "/usr/share/"; + dirList << "/usr/local/share/"; + + QStringList StandardDataDir = + getFullLocation(dirList,shortname, + QDir::FilterSpec(QDir::Files | QDir::Dirs | QDir::Readable), + true, false); + + if (StandardDataDir.isEmpty()) + return; + + for ( QStringList::Iterator it = StandardDataDir.begin(); it != StandardDataDir.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + entry.clear(); + + addAtom(entry, KIO::UDS_NAME, 0, i18n("App Data (%1)").arg(*it)); + addAtom(entry, KIO::UDS_URL, 0, *it); + + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/directory"); + + QString icon = "binaryfolder"; + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); + if ( !entry.isEmpty() ) + list.append(entry); + } +} + + +void AppImpl::createKDEConfigEntry(KIO::UDSEntry &entry, + const QString &shortname) +{ + + entry.clear(); + + QString KDEDataDir = locate("config", shortname+"rc"); + kdDebug() << "KDEDataDir: " << KDEDataDir << endl; + + if (KDEDataDir.isEmpty()) + return; + + addAtom(entry, KIO::UDS_NAME, 0, i18n("Config File (%1)").arg(KDEDataDir)); + addAtom(entry, KIO::UDS_URL, 0, KDEDataDir); + + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "text/plain"); + + QString icon = "configure"; + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); +} + +void AppImpl::createStandardConfigEntry(QValueList<KIO::UDSEntry> &list, KIO::UDSEntry &entry, + const QString &shortname) +{ + + + QStringList dirList; + dirList << "/etc/"; + dirList << "/usr/local/etc/"; + dirList << "/usr/etc/"; + + QStringList StandardConfigFile = + getFullLocation(dirList,shortname, + QDir::FilterSpec(QDir::Files | QDir::Dirs | QDir::Readable), + true, false); + + if (StandardConfigFile.isEmpty()) + return; + + for ( QStringList::Iterator it = StandardConfigFile.begin(); it != StandardConfigFile.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + entry.clear(); + + bool isFolder = false; + QFileInfo fi(*it); + if (fi.isDir()) + isFolder = true; + + kdDebug() << "createStandardConfigEntry: " << *it << endl; + + addAtom(entry, KIO::UDS_URL, 0, *it); + + QString icon; + if (isFolder){ + addAtom(entry, KIO::UDS_NAME, 0, i18n("Config Data(%1)").arg(*it)); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/directory"); + icon = "configfolder"; + }else{ + addAtom(entry, KIO::UDS_NAME, 0, i18n("Config File(%1)").arg(*it)); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFREG); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "text/plain"); + icon = "configure"; + } + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); + if ( !entry.isEmpty() ) + list.append(entry); + } +} + +void AppImpl::createTmpDirEntry(QValueList<KIO::UDSEntry> &list, KIO::UDSEntry &entry, + const QString &shortname) +{ + + + QStringList dirList; + dirList << "/tmp/"; + + QStringList TmpDir = + getFullLocation(dirList,shortname, + QDir::FilterSpec(QDir::Files | QDir::Dirs | QDir::Readable), + true, true); + + if (TmpDir.isEmpty()) + return; + + for ( QStringList::Iterator it = TmpDir.begin(); it != TmpDir.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + entry.clear(); + + bool isFolder = false; + QFileInfo fi(*it); + if (fi.isDir()) + isFolder = true; + + kdDebug() << "createTmpDirEntry: " << *it << endl; + + addAtom(entry, KIO::UDS_URL, 0, *it); + + QString icon; + if (isFolder){ + addAtom(entry, KIO::UDS_NAME, 0, i18n("Tmp Data (%1)").arg(*it)); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFDIR); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "inode/directory"); + icon = "tmpfolder"; + }else{ + addAtom(entry, KIO::UDS_NAME, 0, i18n("Tmp File (%1)").arg(*it)); + addAtom(entry, KIO::UDS_FILE_TYPE, S_IFREG); + addAtom(entry, KIO::UDS_MIME_TYPE, 0, "text/plain"); + icon = "eraser"; + } + addAtom(entry, KIO::UDS_ICON_NAME, 0, icon); + if ( !entry.isEmpty() ) + list.append(entry); + } +} + +void AppImpl::slotEntries(KIO::Job *job, const KIO::UDSEntryList &list) +{ + if (list.size()>0) + { + job->kill(true); + m_lastListingEmpty = false; + qApp->eventLoop()->exitLoop(); + } +} + +void AppImpl::slotResult(KIO::Job *) +{ + qApp->eventLoop()->exitLoop(); +} + +bool AppImpl::listAppContents(const QString &name, QValueList<KIO::UDSEntry> &list) +{ + + + kdDebug() << "AppImpl::listAppEntries" << endl; + KIO::UDSEntry entry; + + //Create entry for binary file + entry.clear(); + createExeEntry(list,entry,name,getAppAddress(name)); +// if ( !entry.isEmpty() ) +// list.append(entry); + + //Create entry for man page + entry.clear(); + createManPageEntry(entry,name); + if ( !entry.isEmpty() ) + list.append(entry); + + //Create entry for apps folder in home dir + entry.clear(); + createHomeDirEntry(list,entry,name); +// if ( !entry.isEmpty() ) +// list.append(entry); + + //Create entry for apps folder in home dir + entry.clear(); + createKDEDataDirEntry(list,entry,name); +// if ( !entry.isEmpty() ) +// list.append(entry); + + //Create entry for apps folder in home dir + entry.clear(); + createStandardDataDirEntry(list,entry,name); +// if ( !entry.isEmpty() ) +// list.append(entry); + + //Create entry for apps folder in home dir + entry.clear(); + createKDEConfigEntry(entry,name); + if ( !entry.isEmpty() ) + list.append(entry); + + //Create entry for apps folder in home dir + entry.clear(); + createStandardConfigEntry(list,entry,name); +// if ( !entry.isEmpty() ) +// list.append(entry); + + //Create entry for apps folder in home dir + entry.clear(); + createTmpDirEntry(list,entry,name); + + return true; +} + +QStringList AppImpl::getAppAddress(const QString &name) +{ + + QStringList dirList; + dirList << "/usr/local/bin/"; + dirList << "/usr/bin/"; + + return getFullLocation(dirList,name, QDir::FilterSpec(QDir::Files | QDir::Readable),false, false); + +} + + +QStringList AppImpl::getFullLocation(const QStringList &dirList, const QString &name, + const QDir::FilterSpec &filter, bool beginswith, bool recursive) +{ + + QStringList finds; + QStringList::ConstIterator dirpath = dirList.begin(); + QStringList::ConstIterator end = dirList.end(); + for(; dirpath!=end; ++dirpath) + { + QDir dir = *dirpath; + if (!dir.exists()) continue; + + QStringList filenames + = dir.entryList( filter ); + + QStringList::ConstIterator filename = filenames.begin(); + QStringList::ConstIterator endf = filenames.end(); + + for(; filename!=endf; ++filename) + { + + if ((recursive) && ((*filename != "..")&&(*filename != "."))){ + QFileInfo fi(*dirpath+*filename); + if (fi.isDir()){ + QStringList recurfinds; + recurfinds = getFullLocation(QStringList(*dirpath+*filename+"/"), + name, filter, beginswith, recursive); + if (!recurfinds.isEmpty()){ + for ( QStringList::Iterator it = recurfinds.begin(); + it != recurfinds.end(); ++it ) + { + if ((*it).isEmpty()) + continue; + finds << (*it); + } + } + } + } + + if ((name == *filename) || + ((beginswith) && (*filename).startsWith(name))) + { + finds << *dirpath+*filename; + } + } + } + return finds; +} + +#include "appimpl.moc" |