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