trial version of "run script" popup menu entry for the GUI
This commit is contained in:
parent
e5dfb800f7
commit
7373f5e58d
3 changed files with 85 additions and 20 deletions
|
@ -77,6 +77,27 @@ QMenu *create(QWidget *me, int opts, RefCntr<DocSequence> 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<DesktopDb::AppDef>::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()));
|
||||
|
|
|
@ -30,21 +30,15 @@ static const string desktopext("desktop");
|
|||
|
||||
static DesktopDb *theDb;
|
||||
|
||||
typedef map<string, vector<DesktopDb::AppDef> > 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<AppDef> *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<AppDef> *apps,
|
|||
return true;
|
||||
}
|
||||
|
||||
bool DesktopDb::allApps(vector<AppDef> *apps)
|
||||
{
|
||||
map<string, AppDef> allaps;
|
||||
for (AppMap::const_iterator it = m_appMap.begin();
|
||||
it != m_appMap.end(); it++) {
|
||||
for (vector<AppDef>::const_iterator it1 = it->second.begin();
|
||||
it1 != it->second.end(); it1++) {
|
||||
allaps.insert(pair<string, AppDef>
|
||||
(it1->name, AppDef(it1->name, it1->command)));
|
||||
}
|
||||
}
|
||||
for (map<string, AppDef>::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
|
||||
|
|
|
@ -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<AppDef> *apps,
|
||||
std::string *reason = 0);
|
||||
|
||||
/**
|
||||
* Get all applications defs:
|
||||
* @param[output] apps applications
|
||||
* @return true
|
||||
*/
|
||||
bool allApps(vector<AppDef> *apps);
|
||||
|
||||
typedef map<string, vector<DesktopDb::AppDef> > 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;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue