added two files with ideas about bright future
[simple-launcher] / misc / BasicItem.h
1 #ifndef __BASICITEM_H__
2 #define __BASICITEM_H__
3
4 #include <string>
5
6 #include <gdk-pixbuf/gdk-pixbuf.h>
7
8 class BasicItem {
9 protected:
10   BasicItem(const std::string& type, const std::string& id) : myType(type), myID(id), myEnabled(false) {}
11 public:
12   virtual ~BasicItem() {}
13
14         const std::string& getType() const { return myType; }
15         const std::string& getID() const { return myID; }
16
17   virtual std::string getName() const = 0;
18   virtual std::string getComment() const = 0;
19   virtual GdkPixbuf *getIcon(int iconSize) const = 0;
20
21   virtual void activate() = 0;
22
23   virtual bool isSane() const = 0;
24
25   bool isEnabled(void) const { return myEnabled; }
26
27   virtual void enable() { myEnabled = isSane(); }
28   virtual void disable() { myEnabled = false; }
29   void toggle() {
30     if (myEnabled) {
31       disable();
32     } else {
33       enable();
34     }
35   }
36 private:
37   BasicItem();  // We do not want people to create these objects :)
38
39         const std::string& myType;
40         const std::string& myID;
41   bool myEnabled;
42 };
43
44 class BasicItemFactory {
45 private:
46         BasicItemFactory();
47
48 public:
49         virtual const string::std& factoryName() const = 0;
50
51         virtual BasicItem *createItem(const std::string&) const = 0;
52
53 protected:
54         static void registerFactory(const std::string&, BasicItemFactory *);
55
56 protected:
57         std::map<std::string, BasicItemFactory *> ourFactories;
58 };
59
60 typedef struct BasicItemCollection {
61   typedef std::vector<std::string> Names;
62   typedef std::map<std::string, BasicItem *> Items;
63
64   Names myNames;
65   Items myItems;
66
67   bool exists(const std::string& name) {
68     return std::find(myNames.begin(), myNames.end(), name) != myNames.end();
69   }
70
71   size_t size() { return myNames.size(); }
72
73   BasicItem *operator[](int index) {
74     return myItems[myNames[index]];
75   }
76
77   std::string& name(int index) {
78     return myNames[index];
79   }
80
81   void add(const std::string& name, BasicItem *item) {
82     myNames.push_back(name);
83     myItems[name] = item;
84   }
85
86   void swap(size_t i1, size_t i2) {
87     std::swap(myNames[i1], myNames[i2]);
88   }
89
90   void clear() {
91     for (Items::iterator it = myItems.begin(); it != myItems.end(); ++it) {
92       if (it->second != NULL) {
93         delete it->second;
94         it->second = NULL;
95       }
96     }
97
98     myNames.resize(0);
99     myItems.clear();
100   }
101
102   BasicItemCollection& operator=(const BasicItemCollection& that) {
103     myNames = that.myNames;
104     myItems = that.myItems;
105
106     return *this;
107   }
108 };
109
110 #endif