icons._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: icons.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 inherit "/kernel/module";
20 #include <macros.h>
21 #include <attributes.h>
22 #include <events.h>
23 #include <classes.h>
24 #include <database.h>
25 #include <exception.h>
26 #include <types.h>
27 //! This is the icon module. It keeps track of all classes and maps
28 //! an icon document for each class. If no individual icon is set in
29 //! an object the OBJ_ICON attribute is acquired from this object and
30 //! so the default icon is retrieved. Obviously this enables the admin
31 //! to replace the default system icons at this place.
32 //!
33 //! @note
34 //! Whats missing here is an interface to set the icons
35 //! which is really not an issue of this module, but an appropriate
36 //! script should do the work and call the functions here.
37 class icons : public module{
38 public:
39 
40 
41 
42 
43 
44 
45  mapping mIcons = ([ ]);
46 
47 #define ATTR_ICONSETS "iconsets"
48 
49 void load_module ()
50 {
51  // remove invalid iconsets:
52  array iconsets = query_attribute( ATTR_ICONSETS ) || ({ });
53  int iconsets_changed = 0;
54  foreach ( iconsets, mixed iconset ) {
55  if ( !objectp(iconset) || iconset->status() == PSTAT_DELETED ||
56  iconset->status() == PSTAT_FAIL_DELETED ) {
57  iconsets -= ({ iconset });
58  iconsets_changed = 1;
59  }
60  }
61  if ( iconsets_changed ) set_attribute( ATTR_ICONSETS, iconsets );
62 }
63 
64 void install_module()
65 {
66 }
67 
68 private:
69 object init_icon ( string path ) {
70  object icon;
71  if ( catch ( icon = _FILEPATH->path_to_object( path ) ) || !objectp(icon) ) {
72  werror( "icons module: could not initialize icon '%s'\n", path );
73  return UNDEFINED;
74  }
75  return icon;
76 }
77 
78 public:
79 
80 private:
81 void init_icons()
82 {
83  mixed err = catch {
84  mIcons = ([
85  CLASS_DATE:
86 private:
87  init_icon("/images/doctypes/type_date.gif"),
88  CLASS_DATE|CLASS_LINK:
89 private:
90  init_icon("/images/doctypes/type_date_lnk.gif"),
91  CLASS_CALENDAR:
92 private:
93  init_icon("/images/doctypes/type_calendar.gif"),
94  CLASS_CALENDAR|CLASS_LINK:
95 private:
96  init_icon("/images/doctypes/type_calendar_lnk.gif"),
97  CLASS_TRASHBIN:
98 private:
99  init_icon("/images/doctypes/trashbin.gif"),
100  CLASS_FACTORY:
101 private:
102  init_icon("/images/doctypes/type_factory.gif"),
103  CLASS_ROOM:
104 private:
105  init_icon("/images/doctypes/type_area.gif"),
106  CLASS_ROOM|CLASS_LINK:
107 private:
108  init_icon("/images/doctypes/type_gate.gif"),
109  CLASS_EXIT:
110 private:
111  init_icon("/images/doctypes/type_gate.gif"),
112  CLASS_EXIT|CLASS_LINK:
113 private:
114  init_icon("/images/doctypes/type_gate.gif"),
115  CLASS_USER:
116 private:
117  init_icon("/images/doctypes/user_unknown.jpg"),
118  CLASS_GROUP:
119 private:
120  init_icon("/images/doctypes/type_group.gif"),
121  CLASS_CONTAINER:
122 private:
123  init_icon("/images/doctypes/type_folder.gif"),
124  CLASS_CONTAINER|CLASS_LINK:
125 private:
126  init_icon("/images/doctypes/type_folder_lnk.gif"),
127  CLASS_DOCUMENT:
128 private:
129  init_icon("/images/doctypes/type_generic.gif"),
130  CLASS_DOCUMENT|CLASS_LINK:
131 private:
132  init_icon("/images/doctypes/type_generic_lnk.gif"),
133  "text/html":
134 private:
135  init_icon("/images/doctypes/type_html.gif"),
136  CLASS_DOCHTML:
137 private:
138  init_icon("/images/doctypes/type_html.gif"),
139  CLASS_DOCHTML|CLASS_LINK:
140 private:
141  init_icon("/images/doctypes/type_html_lnk.gif"),
142  "image/*":
143 private:
144  init_icon("/images/doctypes/type_img.gif"),
145  CLASS_IMAGE:
146 private:
147  init_icon("/images/doctypes/type_img.gif"),
148  CLASS_IMAGE|CLASS_LINK:
149 private:
150  init_icon("/images/doctypes/type_img_lnk.gif"),
151  CLASS_DOCEXTERN:
152 private:
153  init_icon("/images/doctypes/type_references.gif"),
154  CLASS_DOCEXTERN|CLASS_LINK:
155 private:
156  init_icon("/images/doctypes/type_references_lnk.gif"),
157  "source/pike":
158 private:
159  init_icon("/images/doctypes/type_pike.gif"),
160  CLASS_SCRIPT:
161 private:
162  init_icon("/images/doctypes/type_pike.gif"),
163  CLASS_SCRIPT|CLASS_LINK:
164 private:
165  init_icon("/images/doctypes/type_pike_lnk.gif"),
166  CLASS_DOCLPC:
167 private:
168  init_icon("/images/doctypes/type_pike.gif"),
169  CLASS_DOCLPC|CLASS_LINK:
170 private:
171  init_icon("/images/doctypes/type_pike_lnk.gif"),
172  CLASS_OBJECT:
173 private:
174  init_icon("/images/doctypes/type_object.gif"),
175  CLASS_OBJECT|CLASS_LINK:
176 private:
177  init_icon("/images/doctypes/type_object_lnk.gif"),
178  CLASS_MESSAGEBOARD:
179 private:
180  init_icon("/images/doctypes/type_messages.gif"),
181  CLASS_MESSAGEBOARD|CLASS_LINK:
182 private:
183  init_icon("/images/doctypes/type_messages_lnk.gif"),
184  "video/*":
185 private:
186  init_icon("/images/doctypes/type_movie.gif"),
187  "audio/*":
188 private:
189  init_icon("/images/doctypes/type_audio.gif"),
190  "application/pdf":
191 private:
192  init_icon("/images/doctypes/type_pdf.gif"),
193  ]);
194  };
195  if ( err != 0 ) {
196  LOG("While installing Icons-module: One or more images not found !");
197  LOG(sprintf("%s\n%O", err[0], err[1]));
198  mIcons = ([ ]);
199  }
200 
201 public:
202  require_save(STORE_ICONS);
203 }
204 
205 private:
206 void init_module()
207 {
208 
209  add_data_storage(STORE_ICONS, store_icons, restore_icons, 1);
210  if ( !objectp(MODULE_GROUPS) || !objectp(_ROOTROOM) )
211  return; // first start of server
212 
213  if ( _FILEPATH->get_object_in_cont(_ROOTROOM, "images") == 0 ) {
214  LOG("Warning: no /images container found for icons module ...");
215  }
216 
217  set_attribute(OBJ_DESC, "This is the icons module. Here each class "+
218  "is associated an appropriate icon.");
219 
220  LOG("Initializing icons ...");
221  init_icons();
222 }
223 
224 public:
225 
226 bool keep_acquire(object o, mixed key, mixed val)
227 {
228  return false; // drop acquire if icon is set
229 }
230 
231 mixed set_attribute(string|int key, mixed val)
232 {
233  if ( key == OBJ_ICON || intp(key) ) {
234  return 0;
235  }
236  return ::set_attribute(key, val);
237 }
238 
239 mixed query_attribute(string|int key)
240 {
241  if ( key == OBJ_ICON )
242  return get_default_icon( CALLER );
243  return ::query_attribute(key);
244 }
245 
246 array get_iconsets ()
247 {
248  array iconsets = query_attribute( ATTR_ICONSETS ) || ({ });
249  if ( !arrayp(iconsets) )
250  return ({ });
251  return copy_value( iconsets );
252 }
253 
254 void add_iconset ( object iconset )
255 {
256  if ( !functionp( iconset->get_default_icon ) )
257  THROW( "Iconset needs to implement get_default_icon(obj) and "
258  "get_icon_by_name(name) functions.", E_ERROR );
259  array iconsets = query_attribute( ATTR_ICONSETS ) || ({ });
260  if ( search( iconsets, iconset ) >= 0 )
261  return;
262  iconsets += ({ iconset });
263  set_attribute( ATTR_ICONSETS, iconsets );
264 }
265 
266 void remove_iconset ( object iconset )
267 {
268  array iconsets = query_attribute( ATTR_ICONSETS );
269  if ( search( iconsets, iconset ) < 0 )
270  return;
271  iconsets -= ({ iconset });
272  set_attribute( ATTR_ICONSETS, iconsets );
273 }
274 
275 object get_default_icon ( object obj )
276 {
277  foreach ( query_attribute( ATTR_ICONSETS ) || ({ }), object iconset ) {
278  if ( !objectp(iconset) || !functionp(iconset->get_default_icon) ) continue;
279  object icon = iconset->get_default_icon( obj );
280  if ( objectp(icon) )
281  return icon;
282  }
283  return get_icon( obj->get_object_class(),
284  obj->query_attribute( DOC_MIME_TYPE ) );
285 }
286 
287 object get_icon_by_name ( string name )
288 {
289  foreach ( query_attribute( ATTR_ICONSETS ) || ({ }), object iconset ) {
290  if ( !objectp(iconset) || !functionp(iconset->get_icon_by_name) ) continue;
291  object icon = iconset->get_icon_by_name( name );
292  if ( objectp(icon) )
293  return icon;
294  return UNDEFINED;
295  }
296 }
297 
298 /**
299  * Get an icon for a specific object class or mime-type.
300  *
301  * @param int|string type - the object class
302  * @param string|void mtype - the mime-type
303  * @return an icon document.
304  */
305 object get_icon(int type, string|void mtype)
306 {
307  if ( stringp(mtype) )
308  {
309  if ( objectp(mIcons[mtype]) )
310  return mIcons[mtype];
311 
312  // global type registration
313  string glob, loc;
314  sscanf(mtype,"%s/%s", glob, loc);
315  if ( mIcons[glob+"/*"] )
316  return mIcons[glob+"/*"];
317  }
318 
319  int rtype = type;
320  if ( type & CLASS_LINK ) {
321  object caller = CALLER;
322  object link = caller->get_link_object();
323  type = (objectp(link) ? link->get_object_class() : 1);
324  }
325 
326  int t = 0;
327 
328  for ( int i = 31; i >= 0; i-- ) {
329  if ( (type & (1<<i)) && objectp(mIcons[(1<<i)]) ) {
330  t = 1 << i;
331  break;
332  }
333  }
334  if ( t != 0 ) {
335  if ( rtype & CLASS_LINK )
336  return mIcons[t|CLASS_LINK];
337  else
338  return mIcons[t];
339  }
340  return 0;
341 }
342 
343 void set_icon(int|string type, object icon)
344 {
345  mIcons[type] = icon;
346  require_save(STORE_ICONS);
347 }
348 
349 mapping get_icons()
350 {
351  return copy_value(mIcons);
352 }
353 
354 /**
355  * Restore callback function called by _Database to restore data.
356  *
357  * @param mixed data - the data to restore.
358  */
359 private:
360 void restore_icons(mixed data, string|void index)
361 {
362  if (CALLER != _Database )
363  THROW("Caller is not Database !", E_ACCESS);
364 
365  mapping output = ([]);
366 
367  if (!zero_type(index))
368  {
369  if (index[0]=='\"')
370  mIcons[index[1..]] = data;
371  else
372  mIcons[(int)index[1..]] = data;
373  }
374  else
375  {
376  foreach (indices(data), string ndx)
377  {
378  if (ndx[0]=='\"')
379  mIcons[ndx[1..]] = data[ndx];
380  else
381  mIcons[(int)ndx[1..]] = data[ndx];
382  }
383  }
384 }
385 
386 public:
387 
388 /**
389  * Function to save data called by the _Database.
390  *
391  * @return Mapping of icon save data.
392  */
393 private:
394 mixed store_icons(string|void index)
395 {
396  if (CALLER != _Database )
397  THROW("Caller is not Database !", E_ACCESS);
398  if (zero_type(index)) {
399  mapping output = ([]);
400  foreach(indices(mIcons), mixed ndx)
401  {
402  if (stringp(ndx))
403  output["\""+ndx]= mIcons[ndx];
404  else
405  output["#"+(string)ndx]= mIcons[ndx];
406  }
407  return output;
408  }
409  else
410  {
411  if (index[0]=='\"')
412  return mIcons[index[1..]];
413  else
414  return mIcons[(int)index[1..]];
415  }
416 }
417 
418 public:
419 
420 string get_identifier() { return "icons"; }
421 
422 
423 };