1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
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.
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.
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
17 * $Id: icons.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
19 inherit "/kernel/module";
21 #include <attributes.h>
25 #include <exception.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.
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{
45 mapping mIcons = ([ ]);
47 #define ATTR_ICONSETS "iconsets"
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 });
61 if ( iconsets_changed ) set_attribute( ATTR_ICONSETS, iconsets );
69 object init_icon ( string path ) {
71 if ( catch ( icon = _FILEPATH->path_to_object( path ) ) || !objectp(icon) ) {
72 werror( "icons module: could not initialize icon '%s'\n", path );
87 init_icon("/images/doctypes/type_date.gif"),
88 CLASS_DATE|CLASS_LINK:
90 init_icon("/images/doctypes/type_date_lnk.gif"),
93 init_icon("/images/doctypes/type_calendar.gif"),
94 CLASS_CALENDAR|CLASS_LINK:
96 init_icon("/images/doctypes/type_calendar_lnk.gif"),
99 init_icon("/images/doctypes/trashbin.gif"),
102 init_icon("/images/doctypes/type_factory.gif"),
105 init_icon("/images/doctypes/type_area.gif"),
106 CLASS_ROOM|CLASS_LINK:
108 init_icon("/images/doctypes/type_gate.gif"),
111 init_icon("/images/doctypes/type_gate.gif"),
112 CLASS_EXIT|CLASS_LINK:
114 init_icon("/images/doctypes/type_gate.gif"),
117 init_icon("/images/doctypes/user_unknown.jpg"),
120 init_icon("/images/doctypes/type_group.gif"),
123 init_icon("/images/doctypes/type_folder.gif"),
124 CLASS_CONTAINER|CLASS_LINK:
126 init_icon("/images/doctypes/type_folder_lnk.gif"),
129 init_icon("/images/doctypes/type_generic.gif"),
130 CLASS_DOCUMENT|CLASS_LINK:
132 init_icon("/images/doctypes/type_generic_lnk.gif"),
135 init_icon("/images/doctypes/type_html.gif"),
138 init_icon("/images/doctypes/type_html.gif"),
139 CLASS_DOCHTML|CLASS_LINK:
141 init_icon("/images/doctypes/type_html_lnk.gif"),
144 init_icon("/images/doctypes/type_img.gif"),
147 init_icon("/images/doctypes/type_img.gif"),
148 CLASS_IMAGE|CLASS_LINK:
150 init_icon("/images/doctypes/type_img_lnk.gif"),
153 init_icon("/images/doctypes/type_references.gif"),
154 CLASS_DOCEXTERN|CLASS_LINK:
156 init_icon("/images/doctypes/type_references_lnk.gif"),
159 init_icon("/images/doctypes/type_pike.gif"),
162 init_icon("/images/doctypes/type_pike.gif"),
163 CLASS_SCRIPT|CLASS_LINK:
165 init_icon("/images/doctypes/type_pike_lnk.gif"),
168 init_icon("/images/doctypes/type_pike.gif"),
169 CLASS_DOCLPC|CLASS_LINK:
171 init_icon("/images/doctypes/type_pike_lnk.gif"),
174 init_icon("/images/doctypes/type_object.gif"),
175 CLASS_OBJECT|CLASS_LINK:
177 init_icon("/images/doctypes/type_object_lnk.gif"),
180 init_icon("/images/doctypes/type_messages.gif"),
181 CLASS_MESSAGEBOARD|CLASS_LINK:
183 init_icon("/images/doctypes/type_messages_lnk.gif"),
186 init_icon("/images/doctypes/type_movie.gif"),
189 init_icon("/images/doctypes/type_audio.gif"),
192 init_icon("/images/doctypes/type_pdf.gif"),
196 LOG("While installing Icons-module: One or more images not found !");
197 LOG(sprintf("%s\n%O", err[0], err[1]));
202 require_save(STORE_ICONS);
209 add_data_storage(STORE_ICONS, store_icons, restore_icons, 1);
210 if ( !objectp(MODULE_GROUPS) || !objectp(_ROOTROOM) )
211 return; // first start of server
213 if ( _FILEPATH->get_object_in_cont(_ROOTROOM, "images") == 0 ) {
214 LOG("Warning: no /images container found for icons module ...");
217 set_attribute(OBJ_DESC, "This is the icons module. Here each class "+
218 "is associated an appropriate icon.");
220 LOG("Initializing icons ...");
226 bool keep_acquire(object o, mixed key, mixed val)
228 return false; // drop acquire if icon is set
231 mixed set_attribute(string|int key, mixed val)
233 if ( key == OBJ_ICON || intp(key) ) {
236 return ::set_attribute(key, val);
239 mixed query_attribute(string|int key)
241 if ( key == OBJ_ICON )
242 return get_default_icon( CALLER );
243 return ::query_attribute(key);
246 array get_iconsets ()
248 array iconsets = query_attribute( ATTR_ICONSETS ) || ({ });
249 if ( !arrayp(iconsets) )
251 return copy_value( iconsets );
254 void add_iconset ( object iconset )
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 )
262 iconsets += ({ iconset });
263 set_attribute( ATTR_ICONSETS, iconsets );
266 void remove_iconset ( object iconset )
268 array iconsets = query_attribute( ATTR_ICONSETS );
269 if ( search( iconsets, iconset ) < 0 )
271 iconsets -= ({ iconset });
272 set_attribute( ATTR_ICONSETS, iconsets );
275 object get_default_icon ( object obj )
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 );
283 return get_icon( obj->get_object_class(),
284 obj->query_attribute( DOC_MIME_TYPE ) );
287 object get_icon_by_name ( string name )
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 );
299 * Get an icon for a specific object class or mime-type.
301 * @param int|string type - the object class
302 * @param string|void mtype - the mime-type
303 * @return an icon document.
305 object get_icon(int type, string|void mtype)
307 if ( stringp(mtype) )
309 if ( objectp(mIcons[mtype]) )
310 return mIcons[mtype];
312 // global type registration
314 sscanf(mtype,"%s/%s", glob, loc);
315 if ( mIcons[glob+"/*"] )
316 return mIcons[glob+"/*"];
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);
328 for ( int i = 31; i >= 0; i-- ) {
329 if ( (type & (1<<i)) && objectp(mIcons[(1<<i)]) ) {
335 if ( rtype & CLASS_LINK )
336 return mIcons[t|CLASS_LINK];
343 void set_icon(int|string type, object icon)
346 require_save(STORE_ICONS);
351 return copy_value(mIcons);
355 * Restore callback function called by _Database to restore data.
357 * @param mixed data - the data to restore.
360 void restore_icons(mixed data, string|void index)
362 if (CALLER != _Database )
363 THROW("Caller is not Database !", E_ACCESS);
365 mapping output = ([]);
367 if (!zero_type(index))
370 mIcons[index[1..]] = data;
372 mIcons[(int)index[1..]] = data;
376 foreach (indices(data), string ndx)
379 mIcons[ndx[1..]] = data[ndx];
381 mIcons[(int)ndx[1..]] = data[ndx];
389 * Function to save data called by the _Database.
391 * @return Mapping of icon save data.
394 mixed store_icons(string|void index)
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)
403 output["\""+ndx]= mIcons[ndx];
405 output["#"+(string)ndx]= mIcons[ndx];
412 return mIcons[index[1..]];
414 return mIcons[(int)index[1..]];
420 string get_identifier() { return "icons"; }