Parent: [26c7e1] (diff)

Download this file

appformime.h    92 lines (77 with data), 2.9 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Copyright (C) 2014 J.F.Dockes
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _APPFORMIME_H_INCLUDED_
#define _APPFORMIME_H_INCLUDED_
#include <string>
#include <map>
#include <vector>
/**
* Rather strangely, I could not find a reasonably simple piece of
* code which would parse /usr/share/applications to return a list of
* apps for a given mime type. So here goes. Note that the implementation
* is very primitive for now (no use of cache file, no updating once built).
* Also, this is not thread-safe, but could be made so quite easily.
*/
class DesktopDb {
public:
class AppDef {
public:
AppDef(const std::string& nm, const std::string& cmd)
: name(nm), command(cmd)
{}
AppDef() {}
std::string name;
std::string command;
};
/** Build/Get the db for the standard fdo directory */
static DesktopDb* getDb();
/** Constructor for a db based on a non-standard location */
DesktopDb(const string& dir);
/** 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
* @param[output] apps appropriate applications
* @param[output] reason if we fail, an explanation ?
* @return true for no error (apps may still be empty). false if a serious
* problem was detected.
*/
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);
/**
* Get app with given name
*/
bool appByName(const string& nm, AppDef& app);
typedef std::map<std::string, std::vector<DesktopDb::AppDef> > AppMap;
private:
/** This is used by getDb() and builds a db for the standard location */
DesktopDb();
void build(const std::string& dir);
DesktopDb(const DesktopDb &);
DesktopDb& operator=(const DesktopDb &);
AppMap m_appMap;
std::string m_reason;
bool m_ok;
};
#endif /* _APPFORMIME_H_INCLUDED_ */