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