From 7373f5e58d3aec44bec0b1861e81a341a482ce4a Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Sun, 24 Aug 2014 11:15:50 +0200 Subject: [PATCH] trial version of "run script" popup menu entry for the GUI --- src/qtgui/respopup.cpp | 21 ++++++++++++++ src/utils/appformime.cpp | 60 +++++++++++++++++++++++++++------------- src/utils/appformime.h | 24 +++++++++++++++- 3 files changed, 85 insertions(+), 20 deletions(-) diff --git a/src/qtgui/respopup.cpp b/src/qtgui/respopup.cpp index 4c7e10ab..9dcd554f 100644 --- a/src/qtgui/respopup.cpp +++ b/src/qtgui/respopup.cpp @@ -77,6 +77,27 @@ QMenu *create(QWidget *me, int opts, RefCntr source, Rcl::Doc& doc) SLOT(menuOpenWith(QAction *))); } } + + // See if there are any desktop files in $RECOLL_CONFDIR/scripts + // and possibly create a "run script" menu. + aps.clear(); + ddb = new DesktopDb(path_cat(theconfig->getConfDir(), "scripts")); + if (ddb && ddb->allApps(&aps) && !aps.empty()) { + QMenu *sub = popup->addMenu(me->tr("Run Script")); + if (sub) { + for (vector::const_iterator it = aps.begin(); + it != aps.end(); it++) { + QAction *act = new + QAction(QString::fromUtf8(it->name.c_str()), me); + QVariant v(QString::fromUtf8(it->command.c_str())); + act->setData(v); + sub->addAction(act); + } + sub->connect(sub, SIGNAL(triggered(QAction *)), me, + SLOT(menuOpenWith(QAction *))); + } + } + delete ddb; } popup->addAction(me->tr("Copy &File Name"), me, SLOT(menuCopyFN())); diff --git a/src/utils/appformime.cpp b/src/utils/appformime.cpp index 392db1cb..8e989393 100644 --- a/src/utils/appformime.cpp +++ b/src/utils/appformime.cpp @@ -30,21 +30,15 @@ static const string desktopext("desktop"); static DesktopDb *theDb; -typedef map > AppMap; -static AppMap theAppMap; - -static std::string o_reason; -static bool o_ok; - class FstCb : public FsTreeWalkerCB { public: - FstCb(AppMap *appdefs) + FstCb(DesktopDb::AppMap *appdefs) : m_appdefs(appdefs) { } virtual FsTreeWalker::Status processone(const string &, const struct stat *, FsTreeWalker::CbFlag); - AppMap *m_appdefs; + DesktopDb::AppMap *m_appdefs; }; FsTreeWalker::Status FstCb::processone(const string& fn, const struct stat *, @@ -100,27 +94,37 @@ DesktopDb* DesktopDb::getDb() if (theDb == 0) { theDb = new DesktopDb(); } - if (o_ok) + if (theDb && theDb->m_ok) return theDb; return 0; } +void DesktopDb::build(const string& dir) +{ + FstCb procapp(&m_appMap); + FsTreeWalker walker; + if (walker.walk(dir, procapp) != FsTreeWalker::FtwOk) { + m_ok = false; + m_reason = walker.getReason(); + } + m_ok = true; +} + DesktopDb::DesktopDb() { - FstCb procapp(&theAppMap); - FsTreeWalker walker; - if (walker.walk(topappsdir, procapp) != FsTreeWalker::FtwOk) { - o_ok = false; - o_reason = walker.getReason(); - } - o_ok = true; + build(topappsdir); +} + +DesktopDb::DesktopDb(const string& dir) +{ + build(dir); } bool DesktopDb::appForMime(const string& mime, vector *apps, string *reason) { - AppMap::const_iterator it = theAppMap.find(mime); - if (it == theAppMap.end()) { + AppMap::const_iterator it = m_appMap.find(mime); + if (it == m_appMap.end()) { if (reason) *reason = string("No application found for ") + mime; return false; @@ -129,9 +133,27 @@ bool DesktopDb::appForMime(const string& mime, vector *apps, return true; } +bool DesktopDb::allApps(vector *apps) +{ + map allaps; + for (AppMap::const_iterator it = m_appMap.begin(); + it != m_appMap.end(); it++) { + for (vector::const_iterator it1 = it->second.begin(); + it1 != it->second.end(); it1++) { + allaps.insert(pair + (it1->name, AppDef(it1->name, it1->command))); + } + } + for (map::const_iterator it = allaps.begin(); + it != allaps.end(); it++) { + apps->push_back(it->second); + } + return true; +} + const string& DesktopDb::getReason() { - return o_reason; + return m_reason; } #else // TEST_APPFORMIME diff --git a/src/utils/appformime.h b/src/utils/appformime.h index 5213c0d6..d3e6e730 100644 --- a/src/utils/appformime.h +++ b/src/utils/appformime.h @@ -37,10 +37,17 @@ public: std::string command; }; + /** Build/Get the db for the standard fdo directory */ static DesktopDb* getDb(); - static const string& getReason(); + + /** Constructor for a db based on a non-standard location */ + DesktopDb(const string& dir); + ~DesktopDb(); + /** In case of error: what happened ? */ + const string& getReason(); + /** * Get a list of applications able to process a given MIME type. * @param mime MIME type we want the apps for @@ -52,10 +59,25 @@ public: bool appForMime(const std::string& mime, vector *apps, std::string *reason = 0); + /** + * Get all applications defs: + * @param[output] apps applications + * @return true + */ + bool allApps(vector *apps); + + typedef map > AppMap; + private: + /** This is used by getDb() and builds a db for the standard location */ DesktopDb(); + void build(const string& dir); DesktopDb(const DesktopDb &); DesktopDb& operator=(const DesktopDb &); + + AppMap m_appMap; + std::string m_reason; + bool m_ok; };