3d399c780eee9227366ca4cdbf8e285c862cd56f
[simple-launcher] / launcher-item.h
1 // This file is a part of Simple Launcher
2 //
3 // Copyright (C) 2006, 2007, Mikhail Sobolev
4 //
5 // Simple Launcher is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License version 2 as published by
7 // the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Software Foundation, Inc., 51
16 // Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 #ifndef __LAUNCHER_ITEM_H__
19 #define __LAUNCHER_ITEM_H__
20
21 #include <vector>
22 #include <map>
23 #include <string>
24 #include <algorithm>
25
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <gtk/gtkicontheme.h>
28
29 class LauncherItem {
30 public:
31   LauncherItem();
32   virtual ~LauncherItem();
33
34   bool load(const std::string&);
35
36   bool valid() const { return myIsGood; }
37
38   GdkPixbuf *getIcon(int icon_size) const;
39
40   const std::string& getFileName() const { return myFileName; }
41   std::string getName(bool translate = true) const { return translate ? translateString(myName) : myName; }
42   std::string getComment(bool translate = true) const { return translate ? translateString(myComment) : myComment; }
43   const std::string& getService() const { return myService; }
44   const std::string& getExec() const { return myExec; }
45
46   bool isEnabled(void) const { return myEnabled; }
47
48   void enable() { myEnabled = valid() && checkSanity(); }
49   void disable() { myEnabled = false; }
50   void toggle() {
51     if (myEnabled) {
52       disable();
53     } else {
54       enable();
55     }
56   }
57
58 private:
59   std::string translateString(const std::string& what) const;
60
61   bool checkSanity(void) { return !myName.empty() && (!myService.empty() || !myExec.empty()); }
62
63 private:
64   bool myIsGood;
65
66   std::string myFileName, myName, myComment, myIcon, myService, myExec, myTextDomain;
67   bool myEnabled;
68
69   static GtkIconTheme *ourTheme;
70 };
71
72 typedef struct LauncherItems {
73   typedef std::vector<std::string> Names;
74   typedef std::map<std::string, LauncherItem *> Items;
75
76   Names myNames;
77   Items myItems;
78
79   bool exists(const std::string& name) {
80     return std::find(myNames.begin(), myNames.end(), name) != myNames.end();
81   }
82
83   size_t size() { return myNames.size(); }
84
85   LauncherItem *operator[](int index) {
86     return myItems[myNames[index]];
87   }
88
89   std::string& name(int index) {
90     return myNames[index];
91   }
92
93   void add(const std::string& name, LauncherItem *item) {
94     myNames.push_back(name);
95     myItems[name] = item;
96   }
97
98   void swap(size_t i1, size_t i2) {
99     std::swap(myNames[i1], myNames[i2]);
100   }
101
102   void clear() {
103     for (Items::iterator it = myItems.begin(); it != myItems.end(); ++it) {
104       if (it->second != NULL) {
105         delete it->second;
106         it->second = NULL;
107       }
108     }
109
110     myNames.resize(0);
111     myItems.clear();
112   }
113
114   LauncherItems& operator=(const LauncherItems& that) {
115     myNames = that.myNames;
116     myItems = that.myItems;
117
118     return *this;
119   }
120 };
121
122 #endif
123
124 // vim:ts=2:sw=2:et