Implemented save_config().

eiconman recognize plain files besides .desktop files.
This commit is contained in:
Sanel Zukan 2007-05-25 17:45:17 +00:00
parent 4ef5be0c9b
commit 04e1c63cb2
5 changed files with 137 additions and 67 deletions

View File

@ -45,13 +45,11 @@ void icon_delete_cb(fltk::Widget*, void* di) {
edelib::ask(_("Delete '%s' ?"), dicon->label()); edelib::ask(_("Delete '%s' ?"), dicon->label());
} }
DesktopIcon::DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett, int icon_type) : DesktopIcon::DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett) :
fltk::Widget(isett->x, isett->y, ICONSIZE, ICONSIZE), settings(NULL) fltk::Widget(isett->x, isett->y, ICONSIZE, ICONSIZE), settings(NULL)
{ {
EASSERT(gisett != NULL); EASSERT(isett != NULL); EASSERT(gisett != NULL); EASSERT(isett != NULL);
type = icon_type;
lwidth = lheight = 0; lwidth = lheight = 0;
focus = false; focus = false;
micon = false; micon = false;
@ -69,6 +67,8 @@ DesktopIcon::DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett, int ic
settings->name = isett->name; settings->name = isett->name;
settings->cmd = isett->cmd; settings->cmd = isett->cmd;
settings->icon = isett->icon; settings->icon = isett->icon;
settings->type = isett->type;
settings->key_name= isett->key_name;
// x,y are not needed since x(), y() are filled with it // x,y are not needed since x(), y() are filled with it
@ -107,7 +107,7 @@ DesktopIcon::DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett, int ic
pmenu = new fltk::PopupMenu(0, 0, 100, 100); pmenu = new fltk::PopupMenu(0, 0, 100, 100);
pmenu->begin(); pmenu->begin();
if(type == ICON_TRASH) { if(settings->type == ICON_TRASH) {
edelib::Item* it = new edelib::Item(_("&Open")); edelib::Item* it = new edelib::Item(_("&Open"));
it->offset_x(12, 12); it->offset_x(12, 12);
it = new edelib::Item(_("&Properties")); it = new edelib::Item(_("&Properties"));

View File

@ -18,9 +18,6 @@
#include <fltk/Image.h> #include <fltk/Image.h>
#include <fltk/PopupMenu.h> #include <fltk/PopupMenu.h>
#define ICON_NORMAL 1
#define ICON_TRASH 2
class GlobalIconSettings; class GlobalIconSettings;
class IconSettings; class IconSettings;
class MovableIcon; class MovableIcon;
@ -30,7 +27,6 @@ class DesktopIcon : public fltk::Widget {
IconSettings* settings; IconSettings* settings;
const GlobalIconSettings* globals; const GlobalIconSettings* globals;
int type;
int lwidth; int lwidth;
int lheight; int lheight;
bool focus; bool focus;
@ -45,7 +41,7 @@ class DesktopIcon : public fltk::Widget {
void update_label_size(void); void update_label_size(void);
public: public:
DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett, int icon_type = NORMAL); DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett);
~DesktopIcon(); ~DesktopIcon();
virtual void draw(void); virtual void draw(void);
virtual int handle(int event); virtual int handle(int event);
@ -62,6 +58,8 @@ class DesktopIcon : public fltk::Widget {
bool is_focused(void) { return focus; } bool is_focused(void) { return focus; }
fltk::Image* icon_image(void) { return (fltk::Image*)image(); } fltk::Image* icon_image(void) { return (fltk::Image*)image(); }
const IconSettings* get_settings(void) const { return settings; }
}; };

View File

@ -1,32 +1,28 @@
# general settings for desktop
[Desktop] [Desktop]
Color=35 Color = 35
WallpaperUse=1 WallpaperUse = 0
Wallpaper=/home/sanel/wallpapers/Abaddon.png Wallpaper = /home/foo/xxx/baz/tax
# general settings for icons on desktop
[Icons] [Icons]
Label Foreground=7 Label Background = 216
Gridspacing=10 Label Foreground = 7
OneClickExec=0 Label Fontsize = 10
Label Transparent=True Label Maxwidth = 55
Label Background=216 Label Transparent = 1
AutoArrange=0 Label Visible = 1
Label Maxwidth=55 Gridspacing = 10
Label Fontsize=10 OneClickExec = 0
AutoArrange = 1
# positions of each icon on desktop (if icon is visible) [Demo folder]
X = 20
Y = 151
[Home.desktop] [Home.desktop]
X=13 X = 113
Y=8 Y = 16
[System.desktop]
X=12
Y=212
[trash.desktop] [trash.desktop]
X=15 X = 20
Y=106 Y = 20

View File

@ -170,6 +170,8 @@ Desktop::Desktop() : fltk::Window(0, 0, 100, 100, "")
Desktop::~Desktop() { Desktop::~Desktop() {
EDEBUG(ESTRLOC ": Desktop::~Desktop()\n"); EDEBUG(ESTRLOC ": Desktop::~Desktop()\n");
save_config();
if(selbox) if(selbox)
delete selbox; delete selbox;
@ -275,6 +277,12 @@ void Desktop::read_config(void) {
conf.get("Desktop", "Wallpaper", wpath, sizeof(wpath)); conf.get("Desktop", "Wallpaper", wpath, sizeof(wpath));
dsett->wp_path = wpath; dsett->wp_path = wpath;
// keep path but disable wallpaper if file does not exists
if(!edelib::file_exists(wpath)) {
EDEBUG(ESTRLOC ": %s as wallpaper does not exists\n", wpath);
dsett->wp_use = false;
}
} else { } else {
// color is already filled // color is already filled
dsett->wp_use = default_wp_use; dsett->wp_use = default_wp_use;
@ -341,41 +349,67 @@ void Desktop::load_icons(const char* path, edelib::Config& conf) {
edelib::String full_path; edelib::String full_path;
full_path.reserve(256); full_path.reserve(256);
for(unsigned int i = 0; i < lst.size(); i++) { bool can_add = false;
edelib::MimeType mt;
unsigned int sz = lst.size();
for(unsigned int i = 0; i < sz; i++) {
name = lst[i].c_str(); name = lst[i].c_str();
full_path = path;
full_path += '/';
full_path += name;
can_add = false;
IconSettings is;
// see is possible .desktop file, icon, name fields are filled from read_desktop_file()
if(edelib::str_ends(name, ".desktop")) { if(edelib::str_ends(name, ".desktop")) {
if(read_desktop_file(full_path.c_str(), is))
can_add = true;
} else {
// then try to figure out it's mime; if fails, ignore it
if(mt.set(full_path.c_str())) {
/*
* FIXME: MimeType fails for directories
* Temp solution untill that is fixed in edelib
*/
if(edelib::dir_exists(full_path.c_str()))
is.icon = "folder";
else
is.icon = mt.icon_name();
full_path = path; // icon label is name of file
full_path += '/'; is.name = name;
full_path += name; is.type = ICON_FILE;
IconSettings is; can_add = true;
int icon_type; } else {
EDEBUG(ESTRLOC ": Failed mime-type for %s, ignoring...\n", name);
can_add = false;
}
}
if(read_desktop_file(full_path.c_str(), is, icon_type)) { if(can_add) {
is.key_name = name;
// random_pos() is used if X/Y keys are not found
conf.get(name, "X", icon_x, random_pos(w() - 10));
conf.get(name, "Y", icon_y, random_pos(h() - 10));
// store dekstop name for key in saving positions EDEBUG(ESTRLOC ": %s found with: %i %i\n", name, icon_x, icon_y);
is.desktop_name = name; is.x = icon_x;
is.y = icon_y;
// random_pos() is used if X/Y keys are not found DesktopIcon* dic = new DesktopIcon(&gisett, &is);
conf.get(name, "X", icon_x, random_pos(w() - 10)); add_icon(dic);
conf.get(name, "Y", icon_y, random_pos(h() - 10));
EDEBUG(ESTRLOC ": %s found with: %i %i\n", name, icon_x, icon_y);
is.x = icon_x;
is.y = icon_y;
DesktopIcon* dic = new DesktopIcon(&gisett, &is, icon_type);
add_icon(dic);
} else
EDEBUG(ESTRLOC ": Skipped %s section\n", name);
} }
} }
} }
// reads .desktop file content // reads .desktop file content
bool Desktop::read_desktop_file(const char* path, IconSettings& is, int& icon_type) { bool Desktop::read_desktop_file(const char* path, IconSettings& is) {
EASSERT(path != NULL); EASSERT(path != NULL);
if(!edelib::file_exists(path)) { if(!edelib::file_exists(path)) {
@ -398,10 +432,10 @@ bool Desktop::read_desktop_file(const char* path, IconSettings& is, int& icon_ty
* FIXME: any other way to check for trash icons ??? * FIXME: any other way to check for trash icons ???
*/ */
if(dconf.get("Desktop Entry", "EmptyIcon", buff, buffsz)) { if(dconf.get("Desktop Entry", "EmptyIcon", buff, buffsz)) {
icon_type = ICON_TRASH; is.type = ICON_TRASH;
is.icon = buff; is.icon = buff;
} else } else
icon_type = ICON_NORMAL; is.type = ICON_NORMAL;
if(dconf.error() == edelib::CONF_ERR_SECTION) { if(dconf.error() == edelib::CONF_ERR_SECTION) {
EDEBUG(ESTRLOC ": %s is not valid .desktop file\n"); EDEBUG(ESTRLOC ": %s is not valid .desktop file\n");
@ -410,11 +444,11 @@ bool Desktop::read_desktop_file(const char* path, IconSettings& is, int& icon_ty
dconf.get("Desktop Entry", "Icon", buff, buffsz); dconf.get("Desktop Entry", "Icon", buff, buffsz);
if(icon_type == ICON_TRASH) if(is.type == ICON_TRASH)
is.icon2 = buff; is.icon2 = buff;
else { else {
is.icon = buff; is.icon = buff;
icon_type = ICON_NORMAL; is.type = ICON_NORMAL;
} }
EDEBUG(ESTRLOC ": Icon is: %s\n", is.icon.c_str()); EDEBUG(ESTRLOC ": Icon is: %s\n", is.icon.c_str());
@ -430,6 +464,10 @@ bool Desktop::read_desktop_file(const char* path, IconSettings& is, int& icon_ty
* Specs (desktop entry file) said that Type=Link means there must * Specs (desktop entry file) said that Type=Link means there must
* be somewhere URL key. My thoughts is that in this case Exec key * be somewhere URL key. My thoughts is that in this case Exec key
* should be ignored, even if exists. Then I will follow my thoughts. * should be ignored, even if exists. Then I will follow my thoughts.
*
* FIXME: 'Type' should be seen as test for .desktop file; if key
* is not present, then file should not be considered as .desktop. This
* should be checked before all others.
*/ */
if(!dconf.get("Desktop Entry", "Type", buff, buffsz)) { if(!dconf.get("Desktop Entry", "Type", buff, buffsz)) {
EDEBUG(ESTRLOC ": Missing mandatory Type key\n"); EDEBUG(ESTRLOC ": Missing mandatory Type key\n");
@ -462,7 +500,33 @@ bool Desktop::read_desktop_file(const char* path, IconSettings& is, int& icon_ty
} }
void Desktop::save_config(void) { void Desktop::save_config(void) {
// TODO edelib::Config conf;
conf.set("Desktop", "Color", dsett->color);
conf.set("Desktop", "WallpaperUse", dsett->wp_use);
conf.set("Desktop", "Wallpaper", dsett->wp_path.c_str());
conf.set("Icons", "Label Background", gisett.label_background);
conf.set("Icons", "Label Foreground", gisett.label_foreground);
conf.set("Icons", "Label Fontsize", gisett.label_fontsize);
conf.set("Icons", "Label Maxwidth", gisett.label_maxwidth);
conf.set("Icons", "Label Transparent",gisett.label_transparent);
conf.set("Icons", "Label Visible", gisett.label_draw);
conf.set("Icons", "Gridspacing", gisett.gridspacing);
conf.set("Icons", "OneClickExec", gisett.one_click_exec);
conf.set("Icons", "AutoArrange", gisett.auto_arr);
unsigned int sz = icons.size();
const IconSettings* is = NULL;
for(unsigned int i = 0; i < sz; i++) {
is = icons[i]->get_settings();
conf.set(is->key_name.c_str(), "X", icons[i]->x());
conf.set(is->key_name.c_str(), "Y", icons[i]->y());
}
if(!conf.save(CONFIG_NAME))
EDEBUG(ESTRLOC ": Unable to save to %s\n", CONFIG_NAME);
} }
void Desktop::add_icon(DesktopIcon* ic) { void Desktop::add_icon(DesktopIcon* ic) {
@ -643,6 +707,7 @@ void Desktop::drop_source(const char* src, int x, int y) {
is.name = "XXX"; is.name = "XXX";
is.cmd = "(none)"; is.cmd = "(none)";
is.type = ICON_NORMAL;
edelib::MimeType mt; edelib::MimeType mt;
if(!mt.set(src)) { if(!mt.set(src)) {
@ -654,7 +719,7 @@ void Desktop::drop_source(const char* src, int x, int y) {
EDEBUG("---------> %s\n", is.icon.c_str()); EDEBUG("---------> %s\n", is.icon.c_str());
DesktopIcon* dic = new DesktopIcon(&gisett, &is, ICON_NORMAL); DesktopIcon* dic = new DesktopIcon(&gisett, &is);
add_icon(dic); add_icon(dic);
} }

View File

@ -40,17 +40,28 @@ struct GlobalIconSettings {
bool same_size; bool same_size;
}; };
// settings related to .desktop files #define ICON_NORMAL 1 // .desktop file
#define ICON_TRASH 2 // trash.desktop (specific since have two icons for empty/full)
#define ICON_FILE 3 // real file
#define ICON_SYMLINK 4 // symbolic link
/*
* Settings representing related to icon on desktop. To complicate our lives
* (and to, at least, simplify code) it can be:
* - .desktop file content (normal or trash like)
* - real file copied/moved inside ~/Desktop directory
* - symlink in ~/Desktop directory pointing to the real file
*/
struct IconSettings { struct IconSettings {
int x, y; int x, y;
int type;
bool cmd_is_url; // interpret cmd as url, like system:/,trash:/,$HOME bool cmd_is_url; // interpret cmd as url, like system:/,trash:/,$HOME
edelib::String name; edelib::String name;
edelib::String cmd; edelib::String cmd;
edelib::String icon; edelib::String icon;
edelib::String icon2; // for stateable icons, like trash (empty/full) edelib::String icon2; // for stateable icons, like trash (empty/full)
edelib::String desktop_name; // name used as key when storing positions edelib::String key_name; // name used as key when storing positions
}; };
// selection overlay // selection overlay
@ -91,7 +102,7 @@ class Desktop : public fltk::Window {
void init_desktops(void); void init_desktops(void);
void load_icons(const char* path, edelib::Config& conf); void load_icons(const char* path, edelib::Config& conf);
bool read_desktop_file(const char* path, IconSettings& is, int& icon_type); bool read_desktop_file(const char* path, IconSettings& is);
void add_icon(DesktopIcon* ic); void add_icon(DesktopIcon* ic);
void unfocus_all(void); void unfocus_all(void);