1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/* Copyright (C) 2005 sgop@users.sourceforge.net This is free software
* distributed under the terms of the GNU Public License. See the
* file COPYING for details.
*/
/* $Revision: 1.8 $
* $Date: 2008/05/23 14:54:28 $
* $Author: sgop $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <libxml/parser.h>
#include "tree.h"
#include "utils.h"
#include "colors.h"
#include "gui_main.h"
static GList* Colors = NULL;
static int Saver = 0;
static void color_destroy(color_t* color)
{
g_list_foreach(color->ext, (GFunc)g_free, NULL);
if (color->gc) g_object_unref(G_OBJECT(color->gc));
g_free(color);
}
static void color_init(color_t* color, int r, int g, int b, ColorType type)
{
color->ext = NULL;
color->gc = NULL;
color->c.red = r;
color->c.green = g;
color->c.blue = b;
color->type = type;
}
color_t* color_new(int r, int g, int b, ColorType type)
{
color_t* color = g_malloc0(sizeof(*color));
color_init(color, r, g, b, type);
return color;
}
static void color_add_ext(color_t* color, const char* ext)
{
if (color->type == COLOR_EXTENSION)
{
color->ext = g_list_append(color->ext, g_strdup(ext));
}
else
{
g_log("COLOR", G_LOG_LEVEL_WARNING,
"cannot add extension '%s' to type %d", ext, color->type);
}
}
color_t* color_new_exts(const GdkColor* gcolor, const char* exts)
{
color_t* color;
color = color_new(gcolor->red, gcolor->green, gcolor->blue, COLOR_EXTENSION);
if (exts)
{
char* temp = g_strdup(exts);
char* cur = temp;
char* pos;
while ((pos = strchr(cur, ' ')) != NULL)
{
*pos = 0;
color_add_ext(color, cur);
cur = pos+1;
}
color_add_ext(color, cur);
}
return color;
}
static color_t* color_new_with_exts(const char* c, const char* exts)
{
GdkColor gcolor;
gdk_color_parse(c, &gcolor);
return color_new_exts(&gcolor, exts);
}
static void xml_ext_save(const char* ext, xmlNodePtr xnode)
{
xmlNewTextChild(xnode, NULL, "Extension", ext);
}
static void xml_color_save(color_t* color, xmlNodePtr xnode)
{
xmlNodePtr xsub = xmlNewChild(xnode, NULL, "Color", NULL);
char* temp;
switch (color->type)
{
case COLOR_DEFAULT:
xmlNewProp(xsub, "type", "Default");
break;
case COLOR_FOLDER:
xmlNewProp(xsub, "type", "Folder");
break;
case COLOR_MARK1:
xmlNewProp(xsub, "type", "Mark1");
break;
case COLOR_MARK2:
xmlNewProp(xsub, "type", "Mark2");
break;
default:
break;
}
temp = g_strdup_printf("0x%04x", color->c.red);
xmlNewProp(xsub, "r", temp);
g_free(temp);
temp = g_strdup_printf("0x%04x", color->c.green);
xmlNewProp(xsub, "g", temp);
g_free(temp);
temp = g_strdup_printf("0x%04x", color->c.blue);
xmlNewProp(xsub, "b", temp);
g_free(temp);
if (color->type == COLOR_EXTENSION)
g_list_foreach(color->ext, (GFunc)xml_ext_save, xsub);
}
static void colors_save_real(const char* filename)
{
xmlDocPtr xdoc;
xmlNodePtr xroot;
/* g_log("COLOR", G_LOG_LEVEL_DEBUG, "saving '%s'", filename); */
xdoc = xmlNewDoc("1.0");
xroot = xmlNewNode(NULL, "Colors");
xmlDocSetRootElement(xdoc, xroot);
g_list_foreach(Colors, (GFunc)xml_color_save, xroot);
create_dir(filename);
xmlSaveFormatFile(filename, xdoc, 1);
xmlFreeDoc(xdoc);
}
static gboolean colors_save_delayed()
{
const char* home = g_get_home_dir();
char* filename = g_strdup_printf("%s/.gdmap/colors.xml", home);
colors_save_real(filename);
g_free(filename);
if (Saver) g_source_remove(Saver);
Saver = 0;
return FALSE;
}
static void colors_save()
{
if (Saver) return;
Saver = g_timeout_add(100, (GSourceFunc)colors_save_delayed, NULL);
}
static void xml_color_get(xmlDocPtr xfile, xmlNodePtr xnode)
{
xmlChar* attr;
int r=0,g=0,b=0;
color_t* color;
ColorType type = COLOR_EXTENSION;
attr = xmlGetProp(xnode, "type");
if (attr)
{
if (!strcmp(attr, "Default"))
type = COLOR_DEFAULT;
else if (!strcmp(attr, "Folder"))
type = COLOR_FOLDER;
else if (!strcmp(attr, "Mark1"))
type = COLOR_MARK1;
else if (!strcmp(attr, "Mark2"))
type = COLOR_MARK2;
else
g_log("COLOR", G_LOG_LEVEL_WARNING,
"unknown color type '%s'", attr);
xmlFree(attr);
}
attr = xmlGetProp(xnode, "r");
if (attr && !strncmp(attr, "0x", 2))
{
r = strtoul(attr+2, NULL, 16);
}
else
{
r = atoi(attr);
}
if (attr) xmlFree(attr);
attr = xmlGetProp(xnode, "g");
if (attr && !strncmp(attr, "0x", 2))
{
g = strtoul(attr+2, NULL, 16);
}
else
{
g = atoi(attr);
}
if (attr) xmlFree(attr);
attr = xmlGetProp(xnode, "b");
if (attr && !strncmp(attr, "0x", 2))
{
b = strtoul(attr+2, NULL, 16);
}
else
{
b = atoi(attr);
}
if (attr) xmlFree(attr);
color = color_new(r, g, b, type);
if (type == COLOR_EXTENSION)
{
xmlNodePtr xsub = xnode->xmlChildrenNode;
while (xsub != NULL)
{
attr = xmlNodeListGetString(xfile, xsub->xmlChildrenNode, 1);
if (attr)
{
color_add_ext(color, attr);
xmlFree(attr);
}
xsub = xsub->next;
}
}
Colors = g_list_append(Colors, color);
}
static void xml_colors_get(xmlDocPtr xfile, xmlNodePtr xnode)
{
xmlNodePtr xsub = xnode->xmlChildrenNode;
while (xsub != NULL)
{
if (!strcmp(xsub->name, "Color"))
{
xml_color_get(xfile, xsub);
}
xsub = xsub->next;
}
}
static gboolean colors_load(const char* filename)
{
xmlDocPtr xfile;
xmlNodePtr xroot;
if (!g_file_test(filename, G_FILE_TEST_EXISTS)) return FALSE;
/* g_log("COLOR", G_LOG_LEVEL_DEBUG, "loading '%s'", filename); */
xfile = xmlParseFile(filename);
if (!xfile) return FALSE;
xroot = xmlDocGetRootElement(xfile);
if (xroot == NULL)
{
g_log("COLOR", G_LOG_LEVEL_WARNING, "no root element in file: %s", filename);
xmlFreeDoc(xfile);
return FALSE;
}
if (strcmp(xroot->name, "Colors"))
{
g_log("COLOR", G_LOG_LEVEL_WARNING, "Root element != 'Colors'");
xmlFreeDoc(xfile);
return FALSE;
}
xml_colors_get(xfile, xroot);
xmlFreeDoc(xfile);
return TRUE;
}
static void colors_create_default()
{
Colors = g_list_append(Colors, color_new(0xcccc, 0xcccc, 0xcccc, COLOR_DEFAULT));
Colors = g_list_append(Colors, color_new(0xffff, 0xffff, 0x0000, COLOR_MARK1));
Colors = g_list_append(Colors, color_new(0x0f0f, 0xffff, 0xffff, COLOR_MARK2));
Colors = g_list_append(Colors, color_new(0x2222, 0x4444, 0x0000, COLOR_FOLDER));
Colors = g_list_append(Colors, color_new_with_exts("#DD0000", "asf avi mpg mpeg wmv mov"));
Colors = g_list_append(Colors, color_new_with_exts("#FF00FF", "exe msi deb rpm run"));
Colors = g_list_append(Colors, color_new_with_exts("#BB00BB", "dll o lo a so"));
Colors = g_list_append(Colors, color_new_with_exts("#00AA00", "c cc cpp py php php3 php4 php5 js sh bat"));
Colors = g_list_append(Colors, color_new_with_exts("#00FF00", "h hh"));
Colors = g_list_append(Colors, color_new_with_exts("#EEEE00", "eps bmp svg gif png jpg jpeg tif tiff xpm"));
Colors = g_list_append(Colors, color_new_with_exts("#00DDDD", "mp3 ogg flac wav wma m4a"));
Colors = g_list_append(Colors, color_new_with_exts("#66aaff", "mdb doc pdf ps ppt txt po pot xsl xls xml html htm odp ods otp ots ott stc stw sxc sxi sxw hlp tex"));
Colors = g_list_append(Colors, color_new_with_exts("#0000AA", "zip gz tgz rar bz2 7z"));
}
static GdkGC* color_get_gc(color_t* color)
{
if (!color->gc)
{
GtkWidget* win = gui_get_main_win();
color->gc = gdk_gc_new(win->window);
gdk_colormap_alloc_color(gdk_rgb_get_cmap(), &color->c, FALSE, FALSE);
gdk_gc_set_foreground(color->gc, &color->c);
if (color->type == COLOR_MARK1 || color->type == COLOR_MARK2)
gdk_gc_set_function(color->gc, GDK_XOR);
}
return color->gc;
}
static gboolean color_has_ext(color_t* color, const char* ext)
{
GList* dlist;
for (dlist = color->ext; dlist; dlist = dlist->next)
{
const char* temp = dlist->data;
if (!strcasecmp(temp, ext)) return TRUE;
}
return FALSE;
}
static color_t* color_find_by_type(ColorType type)
{
GList* dlist;
for (dlist = Colors; dlist; dlist = dlist->next)
{
color_t* color = dlist->data;
if (color->type == type) return color;
}
return NULL;
}
static color_t* color_find_by_ext(const char* ext)
{
GList* dlist;
for (dlist = Colors; dlist; dlist = dlist->next)
{
color_t* color = dlist->data;
if (color_has_ext(color, ext)) return color;
}
return NULL;
}
GdkGC* color_get_gc_by_type(ColorType type)
{
color_t* color = color_find_by_type(type);
g_assert(color);
return color_get_gc(color);
}
const GdkColor* color_get_by_type(ColorType type)
{
color_t* color = color_find_by_type(type);
g_assert(color);
return &color->c;
}
const GdkColor* color_get_by_ext(const char* ext)
{
color_t* color = color_find_by_ext(ext);
if (color) return &color->c;
else return color_get_by_type(COLOR_DEFAULT);
}
const GdkColor* color_get_by_file(const char* filename)
{
char* suffix = strrchr(filename, '.');
if (!suffix) return color_get_by_type(COLOR_DEFAULT);
else return color_get_by_ext(suffix+1);
}
static void colors_assure_defaults()
{
if (!color_find_by_type(COLOR_FOLDER))
Colors = g_list_prepend(Colors, color_new(0x2200, 0x4400, 0x0000, COLOR_FOLDER));
if (!color_find_by_type(COLOR_MARK2))
Colors = g_list_prepend(Colors, color_new(0x0f00, 0xff00, 0xff00, COLOR_MARK2));
if (!color_find_by_type(COLOR_MARK1))
Colors = g_list_prepend(Colors, color_new(0xff00, 0xff00, 0x0000, COLOR_MARK1));
if (!color_find_by_type(COLOR_DEFAULT))
Colors = g_list_prepend(Colors, color_new(0xcc00, 0xcc00, 0xcc00, COLOR_DEFAULT));
}
GList* colors_get_list()
{
return Colors;
}
void colors_clear()
{
g_list_foreach(Colors, (GFunc)color_destroy, NULL);
g_list_free(Colors);
Colors = NULL;
colors_save();
}
void color_add(color_t* color)
{
Colors = g_list_prepend(Colors, color);
colors_save();
}
void colors_init()
{
const char* home = g_get_home_dir();
char* filename = g_strdup_printf("%s/.gdmap/colors.xml", home);
if (!colors_load(filename))
{
colors_create_default();
}
else
{
colors_assure_defaults();
}
g_free(filename);
}
|