2006-08-20 22:43:09 +04:00
|
|
|
/*
|
2007-03-20 20:19:31 +03:00
|
|
|
* $Id: etip.cpp 1664 2006-06-14 00:21:44Z karijes $
|
2006-08-20 22:43:09 +04:00
|
|
|
*
|
2008-09-22 17:58:31 +04:00
|
|
|
* ede-tip, show some tips!
|
2006-08-20 22:43:09 +04:00
|
|
|
* Part of Equinox Desktop Environment (EDE).
|
2008-01-14 15:02:43 +03:00
|
|
|
* Copyright (c) 2008 EDE Authors.
|
2006-08-20 22:43:09 +04:00
|
|
|
*
|
2008-01-14 15:02:43 +03:00
|
|
|
* This program is licensed under the terms of the
|
|
|
|
* GNU General Public License version 2 or newer.
|
|
|
|
* See COPYING for the details.
|
2006-08-20 22:43:09 +04:00
|
|
|
*/
|
|
|
|
|
2008-09-22 17:58:31 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2008-09-15 15:00:23 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
#include <FL/Fl_Check_Button.H>
|
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Group.H>
|
|
|
|
#include <FL/Fl_Box.H>
|
|
|
|
#include <FL/Fl_Pixmap.H>
|
2008-01-14 15:02:43 +03:00
|
|
|
|
|
|
|
#include <edelib/Nls.h>
|
2009-02-24 17:11:29 +03:00
|
|
|
#include <edelib/Resource.h>
|
2008-01-14 21:11:54 +03:00
|
|
|
#include <edelib/DesktopFile.h>
|
2008-01-14 15:02:43 +03:00
|
|
|
#include <edelib/File.h>
|
|
|
|
#include <edelib/StrUtil.h>
|
2008-01-14 21:11:54 +03:00
|
|
|
#include <edelib/Util.h>
|
|
|
|
#include <edelib/MessageBox.h>
|
|
|
|
#include <edelib/Directory.h>
|
2007-03-20 20:19:31 +03:00
|
|
|
|
|
|
|
#include "icons/hint.xpm"
|
2008-01-14 15:02:43 +03:00
|
|
|
#include "Fortune.h"
|
2006-08-20 22:43:09 +04:00
|
|
|
|
2009-02-24 17:11:29 +03:00
|
|
|
EDELIB_NS_USING(String)
|
|
|
|
EDELIB_NS_USING(DesktopFile)
|
|
|
|
EDELIB_NS_USING(Resource)
|
|
|
|
EDELIB_NS_USING(dir_create_with_parents)
|
|
|
|
EDELIB_NS_USING(user_config_dir)
|
|
|
|
EDELIB_NS_USING(alert)
|
|
|
|
EDELIB_NS_USING(DESK_FILE_TYPE_APPLICATION)
|
|
|
|
|
2009-03-30 19:17:00 +04:00
|
|
|
static Fl_Pixmap image_hint((const char**)hint_xpm);
|
2006-08-20 22:43:09 +04:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
Fl_Window* win;
|
|
|
|
Fl_Check_Button* check_button;
|
|
|
|
Fl_Box* title_box;
|
|
|
|
Fl_Box* tip_box;
|
|
|
|
int curr_tip = 0;
|
2006-08-20 22:43:09 +04:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
FortuneFile* ffile = 0;
|
2009-02-24 17:11:29 +03:00
|
|
|
String fstring;
|
2006-08-20 22:43:09 +04:00
|
|
|
|
2009-02-24 17:11:29 +03:00
|
|
|
#define TITLE_TIPS_NUM 3
|
2008-01-14 15:02:43 +03:00
|
|
|
const char* title_tips[TITLE_TIPS_NUM] = {
|
2009-02-24 17:11:29 +03:00
|
|
|
_("Did you know?"),
|
2008-01-14 15:02:43 +03:00
|
|
|
_("Tip of the Day"),
|
2009-02-24 17:11:29 +03:00
|
|
|
_("Always good to know"),
|
2007-03-20 20:19:31 +03:00
|
|
|
};
|
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
const char* random_title(const char** lst, unsigned int max) {
|
2007-03-20 20:19:31 +03:00
|
|
|
unsigned int val = rand() % max;
|
|
|
|
return lst[val];
|
2006-08-20 22:43:09 +04:00
|
|
|
}
|
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
const char* random_fortune(void) {
|
|
|
|
unsigned int val = rand() % (fortune_num_items(ffile) - 1);
|
|
|
|
curr_tip = val;
|
|
|
|
fortune_get(ffile, val, fstring);
|
|
|
|
return fstring.c_str();
|
|
|
|
}
|
|
|
|
|
2008-01-14 21:11:54 +03:00
|
|
|
FortuneFile* load_fortune_file(void) {
|
2009-02-24 17:11:29 +03:00
|
|
|
String path = Resource::find_data("tips");
|
|
|
|
if(path.empty())
|
2008-01-14 21:11:54 +03:00
|
|
|
return NULL;
|
|
|
|
|
2009-02-24 17:11:29 +03:00
|
|
|
path += "/ede";
|
|
|
|
String dat = path;
|
2008-01-14 21:11:54 +03:00
|
|
|
dat += ".dat";
|
|
|
|
|
2009-02-24 17:11:29 +03:00
|
|
|
return fortune_open(path.c_str(), dat.c_str());
|
2008-01-14 21:11:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-09-22 17:58:31 +04:00
|
|
|
* Save/create ede-tip.desktop inside autostart directory.
|
2008-01-14 21:11:54 +03:00
|
|
|
* Autostart resides in ~/.config/autostart and directory does not
|
|
|
|
* exists, it will be created (only /autostart/, not full path).
|
|
|
|
*
|
|
|
|
* Saving/creating inside /etc/xdg/autostart is not done since
|
|
|
|
* ~/.config/autostart is prefered (see autostart specs) and all
|
|
|
|
* application will be looked there first.
|
|
|
|
*/
|
|
|
|
void write_autostart_stuff(void) {
|
2009-02-24 17:11:29 +03:00
|
|
|
String path = user_config_dir();
|
2008-01-14 21:11:54 +03:00
|
|
|
path += "/autostart";
|
2009-02-24 17:11:29 +03:00
|
|
|
dir_create_with_parents(path.c_str());
|
2008-01-14 21:11:54 +03:00
|
|
|
|
2008-09-22 17:58:31 +04:00
|
|
|
// now see if ede-tip.desktop exists, and update it if do
|
|
|
|
path += "/ede-tip.desktop";
|
2009-02-24 17:11:29 +03:00
|
|
|
DesktopFile conf;
|
2008-01-14 21:11:54 +03:00
|
|
|
|
|
|
|
bool show_at_startup = check_button->value();
|
|
|
|
|
|
|
|
if(!conf.load(path.c_str()))
|
2009-02-24 17:11:29 +03:00
|
|
|
conf.create_new(DESK_FILE_TYPE_APPLICATION);
|
2008-01-14 21:11:54 +03:00
|
|
|
|
|
|
|
// always write these values so someone does not try to play with us
|
|
|
|
conf.set_hidden(show_at_startup);
|
2008-09-22 17:58:31 +04:00
|
|
|
conf.set_name("EDE Tips");
|
|
|
|
conf.set_exec("ede-tip");
|
2008-01-14 21:11:54 +03:00
|
|
|
|
|
|
|
if(conf.save(path.c_str()) == false)
|
2009-02-24 17:11:29 +03:00
|
|
|
alert(_("I'm not able to save %s. Probably I don't have enough permissions to do that ?"), path.c_str());
|
2008-01-14 21:11:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_autostart_stuff(void) {
|
2009-02-24 17:11:29 +03:00
|
|
|
String path = user_config_dir();
|
2008-09-22 17:58:31 +04:00
|
|
|
path += "/autostart/ede-tip.desktop";
|
2008-01-14 21:11:54 +03:00
|
|
|
|
2009-02-24 17:11:29 +03:00
|
|
|
DesktopFile conf;
|
2008-01-14 21:11:54 +03:00
|
|
|
if(!conf.load(path.c_str())) {
|
|
|
|
check_button->value(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(conf.hidden())
|
|
|
|
check_button->value(1);
|
|
|
|
else
|
|
|
|
check_button->value(0);
|
|
|
|
}
|
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
void close_cb(Fl_Widget*, void*) {
|
2007-03-20 20:19:31 +03:00
|
|
|
win->hide();
|
2008-01-14 21:11:54 +03:00
|
|
|
write_autostart_stuff();
|
2006-08-20 22:43:09 +04:00
|
|
|
}
|
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
void next_cb(Fl_Widget*, void*) {
|
|
|
|
if(!ffile)
|
|
|
|
return;
|
|
|
|
|
2007-03-20 20:19:31 +03:00
|
|
|
curr_tip++;
|
2008-01-14 15:02:43 +03:00
|
|
|
if(curr_tip >= (int)fortune_num_items(ffile))
|
2007-03-20 20:19:31 +03:00
|
|
|
curr_tip = 0;
|
2008-01-14 15:02:43 +03:00
|
|
|
|
|
|
|
fortune_get(ffile, curr_tip, fstring);
|
|
|
|
tip_box->label(fstring.c_str());
|
2006-08-20 22:43:09 +04:00
|
|
|
}
|
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
void prev_cb(Fl_Widget*, void*) {
|
|
|
|
if(!ffile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
curr_tip--;
|
|
|
|
if(curr_tip < 0) {
|
|
|
|
curr_tip = fortune_num_items(ffile);
|
2007-03-20 20:19:31 +03:00
|
|
|
curr_tip--;
|
2008-01-14 15:02:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fortune_get(ffile, curr_tip, fstring);
|
|
|
|
tip_box->label(fstring.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
ffile = load_fortune_file();
|
2007-03-20 20:19:31 +03:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
// initialize random number only if we loaded tips
|
|
|
|
if(ffile)
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
win = new Fl_Window(535, 260, _("EDE Tips"));
|
2007-03-20 20:19:31 +03:00
|
|
|
win->begin();
|
2008-01-14 15:02:43 +03:00
|
|
|
Fl_Group* main_group = new Fl_Group(10, 10, 515, 205);
|
|
|
|
main_group->box(FL_DOWN_BOX);
|
2009-08-12 19:26:38 +04:00
|
|
|
main_group->color(FL_WHITE);
|
2008-01-14 15:02:43 +03:00
|
|
|
main_group->begin();
|
|
|
|
Fl_Box* image_box = new Fl_Box(11, 13, 121, 201);
|
|
|
|
image_box->image(image_hint);
|
|
|
|
|
|
|
|
title_box = new Fl_Box(155, 23, 355, 25, random_title(title_tips, TITLE_TIPS_NUM));
|
|
|
|
title_box->labelfont(FL_HELVETICA_BOLD);
|
|
|
|
title_box->align(196|FL_ALIGN_INSIDE);
|
|
|
|
|
|
|
|
tip_box = new Fl_Box(155, 60, 355, 140);
|
|
|
|
tip_box->align(197|FL_ALIGN_INSIDE);
|
|
|
|
|
|
|
|
if(!ffile)
|
|
|
|
tip_box->label(_("I'm unable to correctly load tip files. Please check what went wrong"));
|
|
|
|
else
|
|
|
|
tip_box->label(random_fortune());
|
2007-03-20 20:19:31 +03:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
main_group->end();
|
2007-03-20 20:19:31 +03:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
check_button = new Fl_Check_Button(10, 224, 225, 25, _("Show tips on startup"));
|
|
|
|
check_button->down_box(FL_DOWN_BOX);
|
2007-03-20 20:19:31 +03:00
|
|
|
|
2008-01-14 21:11:54 +03:00
|
|
|
read_autostart_stuff();
|
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
Fl_Button* prev_button = new Fl_Button(240, 224, 90, 25, _("&Previous"));
|
|
|
|
prev_button->callback(prev_cb);
|
2007-03-20 20:19:31 +03:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
Fl_Button* next_button = new Fl_Button(335, 224, 90, 25, _("&Next"));
|
|
|
|
next_button->callback(next_cb);
|
2007-03-20 20:19:31 +03:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
Fl_Button* close_button = new Fl_Button(435, 224, 90, 25, _("&Close"));
|
|
|
|
close_button->callback(close_cb);
|
|
|
|
|
|
|
|
// check_button somehow steal focus
|
|
|
|
close_button->take_focus();
|
2007-03-20 20:19:31 +03:00
|
|
|
|
|
|
|
win->end();
|
2008-01-14 15:02:43 +03:00
|
|
|
win->show(argc, argv);
|
|
|
|
|
|
|
|
Fl::run();
|
|
|
|
|
|
|
|
if(ffile)
|
|
|
|
fortune_close(ffile);
|
2006-08-20 22:43:09 +04:00
|
|
|
|
2008-01-14 15:02:43 +03:00
|
|
|
return 0;
|
2006-08-20 22:43:09 +04:00
|
|
|
}
|