d38a5fa50a56137ab003723a82a9e01cb9256d8d
[simple-launcher] / misc / BasicItem.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 __BASICITEM_H__
19 #define __BASICITEM_H__
20
21 #include <string>
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24
25 class BasicItem {
26 protected:
27   BasicItem(const std::string& type, const std::string& id) : myType(type), myID(id), myEnabled(false) {}
28 public:
29   virtual ~BasicItem() {}
30
31         const std::string& getType() const { return myType; }
32         const std::string& getID() const { return myID; }
33
34   virtual bool load() = 0;
35
36   virtual std::string getName() const = 0;
37   virtual std::string getComment() const = 0;
38   virtual GdkPixbuf *getIcon(int iconSize) const = 0;
39
40   virtual void activate() = 0;
41
42   virtual bool isSane() const = 0;
43
44   bool isEnabled(void) const { return myEnabled; }
45
46   virtual void enable() { myEnabled = isSane(); }
47   virtual void disable() { myEnabled = false; }
48   void toggle() {
49     if (myEnabled) {
50       disable();
51     } else {
52       enable();
53     }
54   }
55 private:
56   BasicItem();  // We do not want people to create these objects :)
57
58         const std::string& myType;
59         const std::string& myID;
60   bool myEnabled;
61 };
62
63 class BasicItemFactory {
64 private:
65         BasicItemFactory();
66
67         virtual ~BasicItemFactory();
68
69 public:
70         static BasicItem *create(const std::string& factoryName, const std::string& itemID);
71
72 public:
73         virtual const string::std& factoryName() const = 0;
74
75 protected:
76         virtual BasicItem *createItem(const std::string&) const = 0;
77
78         static void registerFactory(const std::string&, BasicItemFactory *);
79         static void deRegisterFactory(const std::string&, BasicItemFactory *);
80
81 protected:
82         static std::map<std::string, BasicItemFactory *> ourFactories;
83 };
84
85 struct BasicItemCollection {
86   typedef std::vector<std::string> Names;
87   typedef std::map<std::string, BasicItem *> Items;
88
89   Names myNames;
90   Items myItems;
91
92   bool exists(const std::string& name) {
93     return std::find(myNames.begin(), myNames.end(), name) != myNames.end();
94   }
95
96   size_t size() { return myNames.size(); }
97
98   BasicItem *operator[](int index) {
99     return myItems[myNames[index]];
100   }
101
102   std::string& name(int index) {
103     return myNames[index];
104   }
105
106   void add(const std::string& name, BasicItem *item) {
107     myNames.push_back(name);
108     myItems[name] = item;
109   }
110
111   void swap(size_t i1, size_t i2) {
112     std::swap(myNames[i1], myNames[i2]);
113   }
114
115   void clear() {
116     for (Items::iterator it = myItems.begin(); it != myItems.end(); ++it) {
117       if (it->second != NULL) {
118         delete it->second;
119         it->second = NULL;
120       }
121     }
122
123     myNames.resize(0);
124     myItems.clear();
125   }
126
127   BasicItemCollection& operator=(const BasicItemCollection& that) {
128     myNames = that.myNames;
129     myItems = that.myItems;
130
131     return *this;
132   }
133 };
134
135 #endif