From 27b8bd888a2366e26c8c12d9a040b188b66e5f85 Mon Sep 17 00:00:00 2001 From: Sanel Zukan Date: Wed, 25 Apr 2012 16:28:09 +0000 Subject: [PATCH] Going toward implementing creating icons on desktop --- ede-desktop/IconDialog.cpp | 168 ++++++++++++++++++++++++++++++++++++ ede-desktop/IconDialog.h | 21 +++++ ede-desktop/Jamfile | 1 + ede-desktop/ede-desktop.cpp | 8 +- ede-desktop/ede-desktop.h | 3 +- 5 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 ede-desktop/IconDialog.cpp create mode 100644 ede-desktop/IconDialog.h diff --git a/ede-desktop/IconDialog.cpp b/ede-desktop/IconDialog.cpp new file mode 100644 index 0000000..006a586 --- /dev/null +++ b/ede-desktop/IconDialog.cpp @@ -0,0 +1,168 @@ +/* + * $Id: IconProperties.h 2366 2008-10-02 09:42:19Z karijes $ + * + * ede-desktop, desktop and icon manager + * Part of Equinox Desktop Environment (EDE). + * Copyright (c) 2006-2012 EDE Authors. + * + * This program is licensed under terms of the + * GNU General Public License version 2 or newer. + * See COPYING for details. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "IconDialog.h" +#include "DesktopIcon.h" +#include "ede-desktop.h" + +EDELIB_NS_USING_LIST(9, (str_tolower, icon_chooser, dir_home, build_filename, alert, + ICON_SIZE_HUGE, String, DesktopFile, DESK_FILE_TYPE_APPLICATION)) + +/* it is safe to be globals */ +static Fl_Window *win; +static Fl_Button *img, *browse, *ok, *cancel; +static Fl_Input *name, *comment, *execute; +static Fl_Choice *icon_type; +static String img_path; + +static bool is_empty(const char *str) { + if(!str) return true; + const char *p = str; + + while(*p++) + if(!isspace(*p)) return false; + + return true; +} + +inline bool is_empty_input(Fl_Input *i) { + return is_empty(i->value()); +} + +static void cancel_cb(Fl_Widget*, void*) { + win->hide(); +} + +static void ok_cb(Fl_Widget*, void*) { + if(is_empty_input(name) || is_empty_input(execute) || !img->image()) { + /* do nothing */ + win->hide(); + return; + } + + DesktopFile df; + df.create_new(DESK_FILE_TYPE_APPLICATION); + df.set_name(name->value()); + + if(comment->value()) + df.set_comment(comment->value()); + + if(!img_path.empty() && img_path.length() > 1) { + /* figure out basename */ + const char *s; + char *p, *e; + + s = img_path.c_str(); + p = (char*)strrchr(s, E_DIR_SEPARATOR); + + if(p && *p++) { + /* now remove extension */ + e = (char*)strrchr((const char*)p, '.'); + if(e) *e = '\0'; + + df.set_icon(p); + } + } else { + df.set_icon("empty"); + } + + df.set_exec(execute->value()); + + /* determine filename and save it */ + String file = name->value(); + const char *fp = file.c_str(); + + str_tolower((unsigned char*)fp); + file += ".desktop"; + + /* TODO: let 'Desktop' (class) returns full desktop path */ + String path = build_filename(dir_home().c_str(), "Desktop", file.c_str()); + + /* go through path and replace spaces with '_' */ + for(char *p = (char*)path.c_str(); p && *p; p++) + if(isspace(*p)) *p = '_'; + + /* + * disable watching on folder and explicitly add file (probably as notification will be fired up faster than + * file will be available on that location) + */ + Desktop::instance()->dir_watch_off(); + + if(df.save(path.c_str())) { + /* explictly add file path */ + Desktop::instance()->add_icon_by_path(path.c_str(), NULL); + /* + * wait a second; this would remove event from the queue so watched does not complain how filed + * does not exists + */ + Fl::wait(1); + Desktop::instance()->redraw(); + } else { + alert(_("Unable to create '%s' file. Received error is: %s\n"), path.c_str(), df.strerror()); + } + + Desktop::instance()->dir_watch_on(); + win->hide(); +} + +static void img_browse_cb(Fl_Widget*, void*) { + img_path = icon_chooser(ICON_SIZE_HUGE); + if(img_path.empty()) return; + + Fl_Image* im = Fl_Shared_Image::get(img_path.c_str()); + if(!im) return; + + img->image(im); + img->redraw(); +} + +void icon_dialog_icon_create(void) { + win = new Fl_Window(430, 170, _("Create desktop icon")); + img = new Fl_Button(10, 10, 75, 75); + img->callback(img_browse_cb); + name = new Fl_Input(205, 10, 215, 25, _("Name:")); + comment = new Fl_Input(205, 40, 215, 25, _("Comment:")); + execute = new Fl_Input(205, 70, 185, 25, _("Execute:")); + browse = new Fl_Button(395, 70, 25, 25, "..."); + icon_type = new Fl_Choice(205, 100, 215, 25, _("Type:")); + icon_type->down_box(FL_BORDER_BOX); + + ok = new Fl_Button(235, 135, 90, 25, _("&OK")); + ok->callback(ok_cb); + cancel = new Fl_Button(330, 135, 90, 25, _("&Cancel")); + cancel->callback(cancel_cb); + win->end(); + win->show(); +} + +void icon_dialog_icon_property(DesktopIcon *d) { +} diff --git a/ede-desktop/IconDialog.h b/ede-desktop/IconDialog.h new file mode 100644 index 0000000..e5ab393 --- /dev/null +++ b/ede-desktop/IconDialog.h @@ -0,0 +1,21 @@ +/* + * $Id: IconProperties.h 2366 2008-10-02 09:42:19Z karijes $ + * + * ede-desktop, desktop and icon manager + * Part of Equinox Desktop Environment (EDE). + * Copyright (c) 2006-2012 EDE Authors. + * + * This program is licensed under terms of the + * GNU General Public License version 2 or newer. + * See COPYING for details. + */ + +#ifndef __ICONDIALOG_H__ +#define __ICONDIALOG_H__ + +class DesktopIcon; + +void icon_dialog_icon_create(void); +void icon_dialog_icon_property(DesktopIcon *d); + +#endif diff --git a/ede-desktop/Jamfile b/ede-desktop/Jamfile index 76559f5..6dac854 100644 --- a/ede-desktop/Jamfile +++ b/ede-desktop/Jamfile @@ -17,6 +17,7 @@ SOURCE = ede-desktop.cpp Wallpaper.cpp DesktopIcon.cpp MovableIcon.cpp + IconDialog.cpp IconProperties.cpp ; ObjectC++Flags $(SOURCE) : $(EDELIB_DBUS_INCLUDE) ; diff --git a/ede-desktop/ede-desktop.cpp b/ede-desktop/ede-desktop.cpp index e33f098..bd49193 100644 --- a/ede-desktop/ede-desktop.cpp +++ b/ede-desktop/ede-desktop.cpp @@ -49,6 +49,7 @@ #include "DesktopIcon.h" #include "Utils.h" #include "Wallpaper.h" +#include "IconDialog.h" #define CONFIG_NAME "ede-desktop" #define ICONS_CONFIG_NAME "ede-desktop-icons" @@ -95,9 +96,10 @@ EDELIB_NS_USING(NETWM_CHANGED_CURRENT_WORKSPACE) static void background_conf_cb(Fl_Widget*, void*); static void icons_conf_cb(Fl_Widget*, void*); static void folder_create_cb(Fl_Widget*, void*); +static void launcher_create_cb(Fl_Widget*, void*); MenuItem desktop_menu[] = { - {_("Create &launcher..."), 0, 0}, + {_("Create &launcher..."), 0, launcher_create_cb}, {_("Create &folder..."), 0, folder_create_cb, 0, FL_MENU_DIVIDER}, {_("&Icons settings..."), 0, icons_conf_cb, 0}, {_("&Background..."), 0, background_conf_cb, 0}, @@ -153,6 +155,10 @@ static void folder_create_cb(Fl_Widget*, void*) { alert(_("Unable to create directory '%s'! Please check if directory already exists or you have enough permissions to create it"), dp.c_str()); } +static void launcher_create_cb(Fl_Widget*, void*) { + icon_dialog_icon_create(); +} + static void desktop_message_handler(int action, Window xid, void *data) { switch(action) { case NETWM_CHANGED_CURRENT_WORKSPACE: diff --git a/ede-desktop/ede-desktop.h b/ede-desktop/ede-desktop.h index 47bfed3..bdcfec9 100644 --- a/ede-desktop/ede-desktop.h +++ b/ede-desktop/ede-desktop.h @@ -120,7 +120,6 @@ private: void add_icon(DesktopIcon* ic); - bool add_icon_by_path(const char* path, edelib::Resource* conf); DesktopIcon* find_icon_by_path(const char* path, DesktopIconListIter* pos); bool remove_icon_by_path(const char* path); @@ -154,6 +153,8 @@ public: void read_config(void); + bool add_icon_by_path(const char* path, edelib::Resource* conf); + void update_workarea(void); void area(int& X, int& Y, int& W, int& H) { X = x(); Y = y(); W = w(); H = h(); }