From 04fb190243442e83349f129b523ab747e58100bf Mon Sep 17 00:00:00 2001 From: Michael Krelin Date: Sat, 05 Apr 2008 11:17:33 +0000 Subject: Initial commit into public repository Signed-off-by: Michael Krelin --- (limited to 'src') diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..1210d2d --- a/dev/null +++ b/src/.gitignore @@ -0,0 +1,13 @@ +/napkin +/napkin.o +/.deps +/.libs +/schema.cc +/schema.o +/db.o +/dialogs.o +/sleep_history.o +/sleep_timeline.o +/widgets.o +/COPYING.cc +/COPYING.o diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..1fdae31 --- a/dev/null +++ b/src/Makefile.am @@ -0,0 +1,32 @@ +bin_PROGRAMS = napkin + +AM_CXXFLAGS = ${MODULES_CFLAGS} -I${top_srcdir}/include/ -I${srcdir} +LIBS = ${MODULES_LIBS} \ + ${top_builddir}/lib/libnapkin.la + +noinst_HEADERS = sqlite.h db.h \ + sleep_timeline.h \ + widgets.h dialogs.h \ + sleep_history.h + +napkin_SOURCES = napkin.cc \ + db.cc \ + sleep_timeline.cc \ + widgets.cc dialogs.cc \ + sleep_history.cc \ + schema.cc COPYING.cc +napkin_DEPENDENCIES = \ + ${top_builddir}/lib/libnapkin.la + +EXTRA_DIST = schema.sql + +schema.cc: schema.sql + (\ + echo 'namespace napkin{const char *sql_bootstrap=' &&\ + sed -e 's/^\s\+//' -e 's/\s*--.*$$//' -e 's/^/"/' -e 's/$$/"/' $< &&\ + echo ';}'\ + ) >$@ +COPYING.cc: ${top_srcdir}/COPYING + echo "const char * COPYING =" >$@ || (rm $@;exit 1) + sed 's/"/\\"/g' $< | sed 's/^/\"/' | sed 's/$$/\\n\"/' >>$@ || (rm $@;exit 1) + echo ";" >>$@ || (rm $@;exit 1) diff --git a/src/db.cc b/src/db.cc new file mode 100644 index 0000000..d1e0a85 --- a/dev/null +++ b/src/db.cc @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include "db.h" + +#include "config.h" + +namespace napkin { + + extern const char *sql_bootstrap; + + db_t::db_t() { + const char *h = getenv("HOME"); + if(h) { + datadir = h; + datadir += "/."PACKAGE_NAME"/"; + }else{ + char *cwd = get_current_dir_name(); + if(!cwd) + throw napkin::exception("failed to get_current_dir_name()"); + datadir = cwd; + free(cwd); + datadir += "/."PACKAGE_NAME"/"; + } + if(access(datadir.c_str(),R_OK|W_OK) + && mkdir(datadir.c_str(),0700)) + throw napkin::exception("no access to '"+datadir+"' directory"); + open((datadir+PACKAGE_NAME".db").c_str()); + assert(_D); + char **resp; int nr,nc; char *errm; + if(sqlite3_get_table( + _D, + "SELECT s_tobed FROM sleeps LIMIT 0", + &resp,&nr,&nc,&errm)!=SQLITE_OK) { + if(sqlite3_exec(_D,sql_bootstrap,NULL,NULL,&errm)!=SQLITE_OK) + throw napkin::exception(string("failed to bootstrap sqlite database: ")+errm); + }else + sqlite3_free_table(resp); + } + + void db_t::store(const hypnodata_t& hd) { + sqlite::mem_t S = sqlite3_mprintf( + "INSERT INTO sleeps (" + "s_tobed,s_alarm," + "s_window,s_data_a," + "s_almost_awakes," + "s_timezone" + ") VALUES (" + "%Q,%Q,%d,%d,%Q,%ld" + ")", + hd.w3c_to_bed().c_str(), + hd.w3c_alarm().c_str(), + hd.window,hd.data_a, + hd.w3c_almostawakes().c_str(), + timezone ); + try { + exec(S); + }catch(sqlite::exception& se) { + if(se.rcode==SQLITE_CONSTRAINT) + throw exception_db_already("The record seems to be already in the database"); + throw exception_db("Well, some error occured"); + } + } + + void db_t::remove(const hypnodata_t& hd) { + sqlite::mem_t S = sqlite3_mprintf( + "DELETE FROM sleeps" + " WHERE s_tobed=%Q AND s_alarm=%Q", + hd.w3c_to_bed().c_str(), + hd.w3c_alarm().c_str() ); + exec(S); + } + + void db_t::load(list& rv, + const string& sql) { + sqlite::table_t T; + int nr,nc; + get_table( string( + "SELECT" + " s_tobed, s_alarm," + " s_window, s_data_a," + " s_almost_awakes" + " FROM sleeps" + " "+sql).c_str(),T,&nr,&nc ); + if(nr) { + assert(nc==5); + for(int r=1;r<=nr;++r) { + hypnodata_ptr_t hd(new hypnodata_t()); + hd->set_to_bed(T.get(r,0,nc)); + hd->set_alarm(T.get(r,1,nc)); + hd->set_window(T.get(r,2,nc)); + hd->set_data_a(T.get(r,3,nc)); + hd->set_almost_awakes(T.get(r,4,nc)); + rv.push_back(hd); + } + } + } + +} diff --git a/src/db.h b/src/db.h new file mode 100644 index 0000000..3794eae --- a/dev/null +++ b/src/db.h @@ -0,0 +1,28 @@ +#ifndef __N_DB_H +#define __N_DB_H + +#include +#include +#include +#include "sqlite.h" + +namespace napkin { + using std::string; + using std::list; + + class db_t : public sqlite::db_t { + public: + string datadir; + + db_t(); + + void store(const hypnodata_t& hd); + void remove(const hypnodata_t& hd); + + void load(list& rv, + const string& sql); + }; + +} + +#endif /* __N_DB_H */ diff --git a/src/dialogs.cc b/src/dialogs.cc new file mode 100644 index 0000000..efe0b36 --- a/dev/null +++ b/src/dialogs.cc @@ -0,0 +1,17 @@ +#include "dialogs.h" + +namespace napkin { + namespace gtk { + + hypnoinfo_dialog_t::hypnoinfo_dialog_t(Gtk::Window& w) + : Gtk::Dialog("Sleepy information",w,true/*modal*/,true/*use separator*/) + { + Gtk::VBox *vb = get_vbox(); + vb->set_spacing(2); + vb->add(w_hinfo); + vb->show_all(); + } + + + } +} diff --git a/src/dialogs.h b/src/dialogs.h new file mode 100644 index 0000000..0a7f1b0 --- a/dev/null +++ b/src/dialogs.h @@ -0,0 +1,25 @@ +#ifndef __N_DIALOGS_H +#define __N_DIALOGS_H + +#include +#include +#include "widgets.h" + +namespace napkin { + namespace gtk { + + class hypnoinfo_dialog_t : public Gtk::Dialog { + public: + hypnoinfo_t w_hinfo; + + hypnoinfo_dialog_t(Gtk::Window& w); + + inline void update_data(const hypnodata_ptr_t& hd) { + w_hinfo.update_data(hd); } + }; + + + } +} + +#endif /* __N_DIALOGS_H */ diff --git a/src/napkin.cc b/src/napkin.cc new file mode 100644 index 0000000..d9ba0c9 --- a/dev/null +++ b/src/napkin.cc @@ -0,0 +1,367 @@ +#include +#include +using std::cerr; +using std::endl; +#include +using std::ofstream; +#include +using std::min; +#include +using std::runtime_error; +#include +using std::list; +#include +using std::vector; +#include +using std::string; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "db.h" +#include "sleep_timeline.h" +#include "dialogs.h" +#include "sleep_history.h" + +#include "config.h" + +class napkin_ui : public Gtk::Window { + public: + Gtk::VBox w_outer_box; + Gtk::Statusbar w_status_bar; + napkin::gtk::sleep_history_t w_history; + Glib::RefPtr uiman; + Glib::RefPtr agroup; + napkin::db_t db; + Glib::RefPtr a_remove; + + napkin_ui() + : w_history(db) + { + static char *ui_info = + "" + "" + "" +#ifndef NDEBUG + "" +#endif + "" +#ifndef NDEBUG + "" + "" +#endif + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" +#ifndef NDEBUG + "" + "" +#endif + "" + "" + ""; + agroup = Gtk::ActionGroup::create(); + agroup->add(Gtk::Action::create("menu_sleep","Sleep")); + agroup->add(Gtk::Action::create("menu_sleep_add","Add")); + agroup->add(Gtk::Action::create("sleep_add_from_sleeptracker",Gtk::Stock::CONNECT, + "from sleeptracker","import sleeptracker data from watch"), + Gtk::AccelKey("d"), + sigc::mem_fun(*this,&napkin_ui::on_sleep_add_from_sleeptracker)); +#ifndef NDEBUG + agroup->add(Gtk::Action::create("sleep_add_from_datafile",Gtk::Stock::CONVERT, + "from data file","import sleeptracker data stored in a file"), + sigc::mem_fun(*this,&napkin_ui::on_sleep_add_from_datafile)); +#endif + agroup->add(a_remove=Gtk::Action::create("sleep_remove",Gtk::Stock::REMOVE, + "Remove","remove highlighted sleep event from the database"), + Gtk::AccelKey("delete"), + sigc::mem_fun(*this,&napkin_ui::on_remove)); + agroup->add(Gtk::Action::create("exit",Gtk::Stock::QUIT,"Exit","Exit "PACKAGE_NAME), + Gtk::AccelKey("w"), + sigc::mem_fun(*this,&napkin_ui::on_quit)); + agroup->add(Gtk::Action::create("menu_help","Help")); + agroup->add(Gtk::Action::create("help_about",Gtk::Stock::ABOUT, + "About","About this program"), + sigc::mem_fun(*this,&napkin_ui::on_help_about)); +#ifndef NDEBUG + agroup->add(Gtk::Action::create("debug",Gtk::Stock::INFO,"Debug","debug action"), + sigc::mem_fun(*this,&napkin_ui::on_debug)); +#endif + uiman = Gtk::UIManager::create(); + uiman->insert_action_group(agroup); + add_accel_group(uiman->get_accel_group()); + uiman->add_ui_from_string(ui_info); + Gtk::Widget * mb = uiman->get_widget("/menu_bar"); + if(mb) + w_outer_box.pack_start(*mb,Gtk::PACK_SHRINK); + Gtk::Widget * tb = uiman->get_widget("/tool_bar"); + if(tb) { + static_cast(tb)->set_toolbar_style(Gtk::TOOLBAR_ICONS); + w_outer_box.pack_start(*tb,Gtk::PACK_SHRINK); + } + w_outer_box.pack_start(w_history,true/*expand*/,true/*fill*/); + w_outer_box.pack_end(w_status_bar,false/*expand*/,false/*fill*/); + add(w_outer_box); + set_title(PACKAGE_STRING); + set_default_size(800,600); + show_all(); + w_status_bar.push(" "PACKAGE_STRING); + + refresh_data(); + + w_history.signal_cursor_changed().connect( + sigc::mem_fun(*this,&napkin_ui::on_history_cursor_changed)); + on_history_cursor_changed(); + w_history.signal_double_click().connect( + sigc::mem_fun(*this,&napkin_ui::on_history_double_click)); + } + + void on_help_about() { + Gtk::AboutDialog about; + about.set_authors(vector(1,"Michael Krelin ")); + about.set_copyright("© 2008 Klever Group"); + extern const char *COPYING; + about.set_license(COPYING); + about.set_program_name(PACKAGE_NAME); + about.set_version(VERSION); + about.set_website("http://kin.klever.net/"); + about.set_website_label("Klever Internet Nothings"); + about.set_comments("The Sleeptracker PRO watch support program"); + about.run(); + } + + void on_history_double_click() { + napkin::hypnodata_ptr_t hd = w_history.get_current(); + if(!hd) return; + napkin::gtk::hypnoinfo_dialog_t hid(*this); + hid.update_data(hd); + hid.add_button(Gtk::Stock::OK,Gtk::RESPONSE_OK); + hid.run(); + } + + void refresh_data() { + load_data("ORDER BY s_alarm DESC"); + } + + void load_data(const string& sql) { + list hds; + db.load(hds,sql); + w_history.set_data(hds); + } + + void on_history_cursor_changed() { + a_remove->set_sensitive(w_history.get_current()); + } + + void on_remove() { + napkin::hypnodata_ptr_t hd = w_history.get_current(); + if(!hd) return; + napkin::gtk::hypnoinfo_dialog_t hid(*this); + hid.update_data(hd); + hid.add_button("Remove from the database",Gtk::RESPONSE_OK); + hid.add_button(Gtk::Stock::CANCEL,Gtk::RESPONSE_CANCEL); + if(hid.run() == Gtk::RESPONSE_OK) { + db.remove(*hd); // TODO: handle error + refresh_data(); + } + } + + void on_quit() { + hide(); + } + + void import_data(const napkin::hypnodata_ptr_t& hd) { + napkin::gtk::hypnoinfo_dialog_t hid(*this); + hid.update_data(hd); + hid.add_button("Add to the database",Gtk::RESPONSE_OK); + hid.add_button(Gtk::Stock::CANCEL,Gtk::RESPONSE_CANCEL); + if(hid.run() == Gtk::RESPONSE_OK) { + try { + db.store(*hd); + refresh_data(); + }catch(napkin::exception_db& nedb) { + Gtk::MessageDialog md(*this, + string("Failed to add data to the database... ")+nedb.what(), + false/*use_markup*/,Gtk::MESSAGE_ERROR,Gtk::BUTTONS_OK, + true/*modal*/); + md.run(); + } + } + } + + class st_download_t : public Gtk::Dialog { + public: + Gtk::Label hint, attempt, error; + int nattempt; + napkin::hypnodata_ptr_t rv; + + st_download_t(Gtk::Window& w) + : Gtk::Dialog("Importing data from watch",w,true/*modal*/,false/*use separator*/), + hint("\nImporting data from the sleeptracker...\n\n" + "Set your watch to the 'data' screen " + " and connect to the compuer, if you haven't yet.",0.5,0.5), + attempt("",1,0.5), error("",0,0.5), + nattempt(1), fd(-1) + { + Gtk::VBox *vb = get_vbox(); + vb->set_spacing(10); + hint.set_justify(Gtk::JUSTIFY_CENTER); + vb->pack_start(hint,Gtk::PACK_SHRINK,5); + vb->pack_start(attempt); + vb->pack_start(error); + add_button("Cancel",Gtk::RESPONSE_CANCEL); + vb->show_all(); + } + ~st_download_t() { + if(!(fd<0)) close(fd); + } + + void on_map() { + Gtk::Dialog::on_map(); + initiate_attempt(); + } + + void initiate_attempt() { + Glib::signal_timeout().connect_seconds( + sigc::mem_fun(*this,&st_download_t::try_watch), + 1); + } + void show_error(const napkin::exception& e) { + error.set_use_markup(true); + error.set_markup(string()+ + ""+ + e.what()+""); + } + void next_attempt() { + char tmp[128]; + snprintf(tmp,sizeof(tmp),"Trying again, attempt #%d",++nattempt); + attempt.set_text(tmp); + } + + int fd; + char buffer[512]; + size_t rb; + + bool try_watch() { + try { + fd = napkin::sleeptracker::download_initiate(getenv("SLEEPTRACKER_PORT")); + Glib::signal_timeout().connect_seconds( + sigc::mem_fun(*this,&st_download_t::try_data), + 1); + return false; + }catch(napkin::exception_sleeptracker& nest) { + show_error(nest); + } + next_attempt(); + return true; + } + + bool try_data() { + try { + try { + rb = napkin::sleeptracker::download_finish(fd,buffer,sizeof(buffer)); + }catch(napkin::exception_st_port& nestp) { + fd = -1; + show_error(nestp); + next_attempt(); + initiate_attempt(); + return false; + } + rv = napkin::sleeptracker::decode(buffer,rb); + response(Gtk::RESPONSE_OK); + }catch(napkin::exception_st_data_envelope& neste) { + show_error(neste); + next_attempt(); + initiate_attempt(); + }catch(napkin::exception_sleeptracker& nest) { + show_error(nest); + } + return false; + } + }; + + void on_sleep_add_from_sleeptracker() { + st_download_t sd(*this); + if(sd.run()==Gtk::RESPONSE_OK && sd.rv ) { + sd.hide(); +#ifndef NDEBUG + { + ofstream dfile( + (db.datadir+"/raw-"+napkin::strftime("%Y-%m-%d.st",time(0))).c_str(), + std::ios::binary|std::ios::out|std::ios::trunc); + if(dfile) + dfile.write(sd.buffer,sd.rb); + dfile.close(); + } +#endif + import_data(sd.rv); + } + } + +#ifndef NDEBUG + void on_sleep_add_from_datafile() { + Gtk::FileChooserDialog d("Please select a file", + Gtk::FILE_CHOOSER_ACTION_OPEN); + d.set_transient_for(*this); + d.add_button(Gtk::Stock::CANCEL,Gtk::RESPONSE_CANCEL); + d.add_button(Gtk::Stock::OPEN,Gtk::RESPONSE_OK); + Gtk::FileFilter stfiles; + stfiles.set_name("Sleeptracker files"); + stfiles.add_pattern("*.st"); + d.add_filter(stfiles); + Gtk::FileFilter allfiles; + allfiles.set_name("All files"); + allfiles.add_pattern("*"); + d.add_filter(allfiles); + if(d.run()==Gtk::RESPONSE_OK) { + d.hide(); + + int fd = open(d.get_filename().c_str(),O_RDONLY); + if(fd<0) + throw napkin::exception("failed to open() data"); + unsigned char buffer[512]; + size_t rb = read(fd,buffer,sizeof(buffer)); + close(fd); + if( (rb==(size_t)-1) || rb==sizeof(buffer)) + throw napkin::exception("error reading datafile"); + napkin::hypnodata_ptr_t hd = napkin::sleeptracker::decode(buffer,rb); + import_data(hd); + } + } +#endif + +#ifndef NDEBUG + void on_debug() { + } +#endif +}; + +int main(int argc,char**argv) { + try { + Gtk::Main m(argc,argv); + napkin_ui hui; + m.run(hui); + return 0; + }catch(std::exception& e) { + cerr << "oops: " << e.what() << endl; + } +} diff --git a/src/schema.sql b/src/schema.sql new file mode 100644 index 0000000..f0fc4dd --- a/dev/null +++ b/src/schema.sql @@ -0,0 +1,9 @@ +CREATE TABLE sleeps ( + s_tobed text NOT NULL, -- w3cish timestamp with minute precision + s_alarm text NOT NULL PRIMARY KEY, -- w3cish timestamp with minute precision + s_window integer NOT NULL, -- number of minutes + s_data_a integer NOT NULL, -- number of seconds + s_almost_awakes text NOT NULL, -- [^0-9:TZ-]-separated list of w3cish + -- timestamps with second precision + s_timezone integer NOT NULL -- seconds west of GMT, TODO: make use of it +); diff --git a/src/sleep_history.cc b/src/sleep_history.cc new file mode 100644 index 0000000..1b5ce27 --- a/dev/null +++ b/src/sleep_history.cc @@ -0,0 +1,158 @@ +#include +#include + +#include "sleep_timeline.h" +#include "sleep_history.h" + +namespace napkin { + namespace gtk { + + sleep_history_t::basic_textrenderer::basic_textrenderer() { + property_family().set_value("monospace"); + property_single_paragraph_mode().set_value(true); + } + void sleep_history_t::basic_textrenderer::render_vfunc( + const Glib::RefPtr& window, Gtk::Widget& widget, + const Gdk::Rectangle& background_area, const Gdk::Rectangle& cell_area, + const Gdk::Rectangle& expose_area, Gtk::CellRendererState flags) { + hypnodata_t *hd = (hypnodata_t*)property_user_data().get_value(); + property_text().set_value(hd?get_text(*hd).c_str():""); + Gtk::CellRendererText::render_vfunc(window,widget, + background_area,cell_area,expose_area, + flags); + } + + const string sleep_history_t::date_render_t::get_text(const hypnodata_t& hd) const { + return hd.str_date(); } + const string sleep_history_t::tobed_render_t::get_text(const hypnodata_t& hd) const { + return hd.str_to_bed(); } + const string sleep_history_t::alarm_render_t::get_text(const hypnodata_t& hd) const { + return hd.str_alarm(); } + sleep_history_t::window_render_t::window_render_t() { + property_xalign().set_value(1); } + const string sleep_history_t::window_render_t::get_text( + const hypnodata_t& hd) const { + char tmp[16]; + snprintf(tmp,sizeof(tmp),"%2d",hd.window); + return tmp; + } + sleep_history_t::nawakes_render_t::nawakes_render_t() { + property_xalign().set_value(1); } + const string sleep_history_t::nawakes_render_t::get_text( + const hypnodata_t& hd) const { + char tmp[16]; + snprintf(tmp,sizeof(tmp),"%2d",(int)hd.almost_awakes.size()); + return tmp; + } + const string sleep_history_t::data_a_render_t::get_text(const hypnodata_t& hd) const { + return hd.str_data_a(); } + + sleep_history_t::sleep_timeline_render_t::sleep_timeline_render_t( + const sleep_history_t& sh) : sleep_history(sh) + { + property_xpad().set_value(2); property_ypad().set_value(2); + } + void sleep_history_t::sleep_timeline_render_t::render_vfunc( + const Glib::RefPtr& window, Gtk::Widget&/*widget*/, + const Gdk::Rectangle&/*background_area*/, const Gdk::Rectangle& cell_area, + const Gdk::Rectangle&/*expose_area*/, Gtk::CellRendererState/*flags*/) { + hypnodata_t *hd = (hypnodata_t*)property_user_data().get_value(); + if(!hd) return; + int xpad = property_xpad(), ypad = property_ypad(); + int x0 = cell_area.get_x()+xpad, y0 = cell_area.get_y()+ypad, + dx = cell_area.get_width()-2*xpad, dy = cell_area.get_height()-2*ypad; + time_t a = hd->aligned_start(); + gtk::render_sleep_timeline( + *hd, + window, + x0,y0, dx,dy, + a+sleep_history.min_tobed, + a+sleep_history.max_alarm ); + } + + sleep_history_t::sleep_history_t(db_t& d) + : store( Gtk::ListStore::create(cols) ), + r_sleep_timeline(*this), + min_tobed(24*60*60*2), max_alarm(0), + db(d) + { + w_tree.set_model(store); + add(w_tree); + append_c("Date",r_date); + append_c("To bed",r_to_bed); + (c_timeline=append_c("Sleep timeline",r_sleep_timeline))->set_expand(true); + append_c("Alarm",r_alarm); + append_c("Window",r_window); + append_c(" N",r_nawakes); + append_c("Data A",r_data_a); + + w_tree.signal_query_tooltip().connect( + sigc::mem_fun(*this,&sleep_history_t::on_query_tooltip)); + w_tree.set_has_tooltip(true); + w_tree.signal_button_press_event().connect( + sigc::mem_fun(*this,&sleep_history_t::on_button_press),false); + } + + Gtk::TreeView::Column *sleep_history_t::append_c(const string& title,Gtk::CellRenderer& renderer) { + Gtk::TreeView::Column *rv = w_tree.get_column(w_tree.append_column(title,renderer)-1); + rv->add_attribute(renderer.property_user_data(),cols.c_hypnodata_ptr); + rv->set_resizable(true); + return rv; + } + + bool sleep_history_t::on_button_press(GdkEventButton* geb) { + if(geb->type!=GDK_2BUTTON_PRESS) return false; + double_click_signal(); + return true; + } + + bool sleep_history_t::on_query_tooltip( + int x,int y,bool keyboard_tooltip, + const Glib::RefPtr& tooltip) { + if(keyboard_tooltip) return false; + int tx,ty; + w_tree.convert_widget_to_tree_coords(x,y,tx,ty); + Gtk::TreeModel::Path p; + Gtk::TreeViewColumn *c; + int cx, cy; + if(!w_tree.get_path_at_pos(tx,ty,p,c,cx,cy)) return false; + if(c != c_timeline) return false; + hypnodata_ptr_t hd = store->get_iter(p)->get_value(cols.c_hypnodata); + string mup = "Almost awake moments are:\n\n"; + for(vector::const_iterator i=hd->almost_awakes.begin();i!=hd->almost_awakes.end();++i) + mup += strftime(" %H:%M:%S\n",*i); + mup += ""; + tooltip->set_markup( mup ); + return true; + } + + void sleep_history_t::set_data(list data) { + store->clear(); + min_tobed = 24*60*60*2; max_alarm = 0; + for(std::list::const_iterator + i=data.begin(); i!=data.end(); ++i) { + Gtk::TreeModel::Row r = *(store->append()); + r[cols.c_hypnodata] = *i; + r[cols.c_hypnodata_ptr] = i->get(); + time_t a = (*i)->aligned_start(); + time_t soff = (*i)->to_bed-a; + if(soff < min_tobed) min_tobed = soff; + time_t eoff = (*i)->alarm-a; + if(eoff > max_alarm) max_alarm = eoff; + } + while(Gtk::Main::events_pending()) Gtk::Main::iteration() ; + w_tree.columns_autosize(); + } + + const hypnodata_ptr_t sleep_history_t::get_current() { + Gtk::TreeModel::Path p; + Gtk::TreeViewColumn *c; + w_tree.get_cursor(p,c); + if( (!p.gobj()) || p.empty()) + return hypnodata_ptr_t(); /* XXX: or throw? */ + Gtk::ListStore::iterator i = store->get_iter(p); + return i->get_value(cols.c_hypnodata); + } + + } +} diff --git a/src/sleep_history.h b/src/sleep_history.h new file mode 100644 index 0000000..7837711 --- a/dev/null +++ b/src/sleep_history.h @@ -0,0 +1,113 @@ +#ifndef __N_SLEEP_HISTORY_H +#define __N_SLEEP_HISTORY_H + +#include +#include +#include +#include +#include +#include "db.h" + +namespace napkin { + namespace gtk { + using std::string; + + class sleep_history_t : public Gtk::ScrolledWindow { + public: + class basic_textrenderer : public Gtk::CellRendererText { + public: + basic_textrenderer(); + void render_vfunc( + const Glib::RefPtr& window, Gtk::Widget& widget, + const Gdk::Rectangle& background_area, const Gdk::Rectangle& cell_area, + const Gdk::Rectangle& expose_area, Gtk::CellRendererState flags); + virtual const string get_text(const hypnodata_t& hd) const = 0; + }; + + class date_render_t : public basic_textrenderer { + public: + const string get_text(const hypnodata_t& hd) const; + }; + class tobed_render_t : public basic_textrenderer { + public: + const string get_text(const hypnodata_t& hd) const; + }; + class alarm_render_t : public basic_textrenderer { + public: + const string get_text(const hypnodata_t& hd) const; + }; + class window_render_t : public basic_textrenderer { + public: + window_render_t(); + const string get_text(const hypnodata_t& hd) const; + }; + class nawakes_render_t : public basic_textrenderer { + public: + nawakes_render_t(); + const string get_text(const hypnodata_t& hd) const; + }; + class data_a_render_t : public basic_textrenderer { + public: + const string get_text(const hypnodata_t& hd) const; + }; + + class sleep_timeline_render_t : public Gtk::CellRenderer { + public: + const sleep_history_t& sleep_history; + + sleep_timeline_render_t(const sleep_history_t& sh); + void render_vfunc(const Glib::RefPtr& window, Gtk::Widget&/*widget*/, + const Gdk::Rectangle&/*background_area*/, const Gdk::Rectangle& cell_area, + const Gdk::Rectangle&/*expose_area*/, Gtk::CellRendererState/*flags*/); + }; + + class columns_t : public Gtk::TreeModel::ColumnRecord { + public: + Gtk::TreeModelColumn c_hypnodata; + Gtk::TreeModelColumn c_hypnodata_ptr; + + columns_t() { + add(c_hypnodata); add(c_hypnodata_ptr); + } + }; + + columns_t cols; + Gtk::TreeView w_tree; + Glib::RefPtr store; + date_render_t r_date; + tobed_render_t r_to_bed; + alarm_render_t r_alarm; + window_render_t r_window; + nawakes_render_t r_nawakes; + data_a_render_t r_data_a; + sleep_timeline_render_t r_sleep_timeline; + Gtk::TreeView::Column *c_timeline; + + sigc::signal double_click_signal; + sigc::signal& signal_double_click() { return double_click_signal; } + + time_t min_tobed, max_alarm; + + db_t& db; + + sleep_history_t(db_t& d); + + Gtk::TreeView::Column *append_c(const string& title,Gtk::CellRenderer& renderer); + + bool on_button_press(GdkEventButton* geb); + bool on_query_tooltip(int x,int y,bool keyboard_tooltip, const Glib::RefPtr& tooltip); + + + void set_data(list data); + + Glib::SignalProxy0 signal_cursor_changed() { + return w_tree.signal_cursor_changed(); + } + + const hypnodata_ptr_t get_current(); + + }; + } +} + +#endif /* __N_SLEEP_HISTORY_H */ diff --git a/src/sleep_timeline.cc b/src/sleep_timeline.cc new file mode 100644 index 0000000..e5d4146 --- a/dev/null +++ b/src/sleep_timeline.cc @@ -0,0 +1,100 @@ +#include +#include +#include + +#include "sleep_timeline.h" + +namespace napkin { + namespace gtk { + using std::vector; + using std::min; + + void render_sleep_timeline( + const hypnodata_t& hd, + const Glib::RefPtr& w, + int x0,int y0,int dx,int dy, + time_t _t0,time_t _t1) { + static Gdk::Color c_tobed("darkgreen"), c_alarm("red"), + c_almostawake("maroon"), c_midnight("blue"), c_hour("#606060"), + c_timeline("#404040"), c_window("red"), + c_background("#ffffc0"), c_border("gray"); + static bool beenthere=false; + if(!beenthere) { + Glib::RefPtr cm(Gtk::Widget::get_default_colormap()); + cm->alloc_color(c_tobed); cm->alloc_color(c_alarm); + cm->alloc_color(c_almostawake); + cm->alloc_color(c_midnight); cm->alloc_color(c_hour); + cm->alloc_color(c_timeline); cm->alloc_color(c_window); + cm->alloc_color(c_background); cm->alloc_color(c_border); + beenthere = true; + } + Glib::RefPtr gc = Gdk::GC::create(w); + + gc->set_foreground(c_background); + w->draw_rectangle(gc,true, x0,y0, dx,dy+1 ); + gc->set_foreground(c_border); + w->draw_rectangle(gc,false, x0,y0, dx,dy+1 ); + x0+=3; dx-=6; + + time_t t0, t1; + if(_t0 && _t1 && _t0!=_t1 && _t0<=hd.to_bed && hd.alarm<=_t1) + t0 = _t0, t1 = _t1; + else + t0 = hd.to_bed, t1 = hd.alarm; + time_t dt = t1-t0; + + time_t tb = hd.to_bed; time_t ta = hd.alarm; + int xb = x0+dx*(tb-t0)/dt, + xa = x0+dx*(ta-t0)/dt; + int ym = y0+dy/2; + gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER); + gc->set_foreground(c_timeline); + w->draw_line(gc, xb,ym, xa,ym ); + time_t ws = ta-hd.window*60; + int xws = x0+dx*(ws-t0)/dt; + gc->set_foreground(c_window); + w->draw_rectangle(gc, true, xws,ym-1, xa-xws,3 ); + gc->set_foreground(c_almostawake); + int tl2 = min(dy/2 - 3, 7); + int yt0 = ym-tl2, yt1 = ym+tl2+1; + for(vector::const_iterator i=hd.almost_awakes.begin();i!=hd.almost_awakes.end();++i) { + int x = x0+dx*(*i-t0)/dt; + w->draw_line(gc, x,yt0, x,yt1 ); + } + tl2 = min(dy/5, 5); + yt0 = ym-tl2; yt1 = ym+tl2+1; + gc->set_foreground(c_hour); + time_t midnight = hd.aligned_start()+24*60*60; + for(time_t h = tb-tb%3600 + 3600; hset_foreground(c_midnight); + int x = x0+dx*(h-t0)/dt; + w->draw_line(gc, x,yt0, x,yt1 ); + if(h==midnight) gc->set_foreground(c_hour); + } + gc->set_line_attributes(2,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER); + gc->set_foreground(c_tobed); + w->draw_line(gc, xb,yt0, xb,yt1 ); + gc->set_foreground(c_alarm); + w->draw_line(gc, xa,yt0, xa,yt1 ); + } + + + bool sleep_timeline_t::on_expose_event(GdkEventExpose*) { + if(!hd) return true; + Glib::RefPtr w = get_window(); + int x0,y0,dx,dy,wd; + w->get_geometry(x0,y0,dx,dy,wd); + render_sleep_timeline( + *hd, + w, + 0,0, dx-2,dy-2 ); + return true; + } + + void sleep_timeline_t::set_data(const hypnodata_ptr_t& _hd) { + hd = _hd; + queue_draw(); + } + + } +} diff --git a/src/sleep_timeline.h b/src/sleep_timeline.h new file mode 100644 index 0000000..3264fd6 --- a/dev/null +++ b/src/sleep_timeline.h @@ -0,0 +1,29 @@ +#ifndef __N_SLEEP_TIMELINE_H +#define __N_SLEEP_TIMELINE_H + +#include +#include +#include + +namespace napkin { + namespace gtk { + + void render_sleep_timeline( + const hypnodata_t& hd, + const Glib::RefPtr& w, + int x0,int y0,int dx,int dy, + time_t _t0=0,time_t _t1=0); + + class sleep_timeline_t : public Gtk::DrawingArea { + public: + hypnodata_ptr_t hd; + + bool on_expose_event(GdkEventExpose*); + void set_data(const hypnodata_ptr_t& _hd); + }; + + + } +} + +#endif /* __N_SLEEP_TIMELINE_H */ diff --git a/src/sqlite.h b/src/sqlite.h new file mode 100644 index 0000000..ad276ee --- a/dev/null +++ b/src/sqlite.h @@ -0,0 +1,103 @@ +#ifndef __SQLITE_H +#define __SQLITE_H + +#include +#include +#include +#include + +namespace sqlite { + using std::string; + + class exception : public std::runtime_error { + public: + int rcode; + explicit exception(const string& w,int rc=-1) + : std::runtime_error(w), rcode(rc) { } + ~exception() throw() { } + }; + + class db_t { + public: + sqlite3 *_D; + + db_t() + : _D(0) { } + db_t(const char *f) + : _D(0) { open(f); } + ~db_t() { close(); } + + operator const sqlite3*(void) const { return _D; } + operator sqlite3*(void) { return _D; } + + void close() { + if(_D) { + sqlite3_close(_D); + _D = 0; + } + } + void open(const char *f) { + close(); + int r = sqlite3_open(f,&_D); + if(r!=SQLITE_OK) { + string msg = sqlite3_errmsg(_D); sqlite3_close(_D); + throw exception("Failed to open SQLite database: "+msg,r); + } + } + + void exec(const char *sql) { + assert(_D); + char *errm; + int r = sqlite3_exec(_D,sql,NULL,NULL,&errm); + if(r!=SQLITE_OK) + throw exception(string("Failed to sqlite3_exec():")+errm,r); + } + void get_table(const char *sql,char ***resp,int *nr,int *nc) { + assert(_D); + char *errm; + int r = sqlite3_get_table(_D,sql,resp,nr,nc,&errm); + if(r!=SQLITE_OK) + throw exception(string("Failed to sqlite3_get_table():")+errm,r); + } + }; + + template + class mem_t { + public: + T _M; + + mem_t(T M) :_M(M) { } + ~mem_t() { if(_M) sqlite3_free(_M); } + + operator const T&(void) const { return _M; } + operator T&(void) { return _M; } + + mem_t operator=(T M) { + if(_M) sqlite3_free(_M); + _M = M; + } + }; + + class table_t { + public: + char **_T; + + table_t() : _T(0) { } + table_t(char **T) : _T(T) { } + ~table_t() { if(_T) sqlite3_free_table(_T); } + + operator char**&(void) { return _T; } + + operator char ***(void) { + if(_T) sqlite3_free_table(_T); + return &_T; } + + const char *get(int r,int c,int nc) { + assert(_T); + return _T[r*nc+c]; + } + }; + +} + +#endif /* __SQLITE_H */ diff --git a/src/widgets.cc b/src/widgets.cc new file mode 100644 index 0000000..ea85bc8 --- a/dev/null +++ b/src/widgets.cc @@ -0,0 +1,63 @@ +#include +#include "widgets.h" + +namespace napkin { + namespace gtk { + + hypnoinfo_t::hypnoinfo_t() + : w_upper(4,3,false/*homogeneous*/), + lc_tobed("To bed:",0.5,0.5), + lc_timeline("Sleep timeline:",0.5,0.5), + lc_alarm("Alarm:",0.5,0.5), lc_window("Window:",0.5,0.5), + l_data_a("",0.9,0.5) + { + add(l_date); + add(l_hseparator); + w_upper.set_col_spacings(5); + w_upper.attach(lc_tobed,0,1,0,1, Gtk::SHRINK); + w_upper.attach(lc_timeline,1,2,0,1, Gtk::SHRINK); + w_upper.attach(lc_alarm,2,3,0,1, Gtk::SHRINK); + w_upper.attach(lf_tobed,0,1,1,4, Gtk::SHRINK); + w_upper.attach(st_timeline,1,2,1,4, + Gtk::FILL|Gtk::EXPAND,Gtk::FILL|Gtk::EXPAND,0,0); + w_upper.attach(lf_alarm,2,3,1,2, Gtk::SHRINK); + w_upper.attach(lc_window,2,3,2,3, Gtk::SHRINK); + w_upper.attach(lf_window,2,3,3,4, Gtk::SHRINK); + add(w_upper); + add(lc_almost_awakes); + add(lf_almost_awakes); + add(l_data_a); + show_all(); + } + + void hypnoinfo_t::update_data(const hypnodata_ptr_t& hd) { + l_date.set_use_markup(true); + l_date.set_markup(""+hd->str_date()+""); + lf_tobed.set_use_markup(true); + lf_tobed.set_markup(""+hd->str_to_bed()+""); + lf_alarm.set_use_markup(true); + lf_alarm.set_markup(""+hd->str_alarm()+""); + char tmp[64]; + snprintf(tmp,sizeof(tmp),"%d mins",hd->window); + lf_window.set_use_markup(true); + lf_window.set_markup(tmp); + snprintf(tmp,sizeof(tmp),"%d almost awake moments:",(int)hd->almost_awakes.size()); + lc_almost_awakes.set_use_markup(true); + lc_almost_awakes.set_markup(tmp); + string awlist; + for(vector::const_iterator i=hd->almost_awakes.begin();i!=hd->almost_awakes.end();++i) { + if(!awlist.empty()) + awlist += ", "; + awlist += strftime("%H:%M:%S",*i); + } + lf_almost_awakes.set_use_markup(true); + lf_almost_awakes.set_line_wrap(true); + lf_almost_awakes.set_line_wrap_mode(Pango::WRAP_WORD); + lf_almost_awakes.set_markup(""+awlist+""); + l_data_a.set_use_markup(true); + l_data_a.set_markup("Data A is "+hd->str_data_a()+""); + st_timeline.set_data(hd); + } + + } +} diff --git a/src/widgets.h b/src/widgets.h new file mode 100644 index 0000000..99936ff --- a/dev/null +++ b/src/widgets.h @@ -0,0 +1,33 @@ +#ifndef __N_WIDGETS_H +#define __N_WIDGETS_H + +#include +#include +#include +#include +#include "sleep_timeline.h" + +namespace napkin { + namespace gtk { + + class hypnoinfo_t : public Gtk::VBox { + public: + Gtk::Label l_date; + Gtk::HSeparator l_hseparator; + Gtk::Table w_upper; + Gtk::Label lc_tobed, lc_timeline, lc_alarm, lc_window; + Gtk::Label lf_tobed, lf_alarm, lf_window; + sleep_timeline_t st_timeline; + Gtk::Label lc_almost_awakes; + Gtk::Label lf_almost_awakes; + Gtk::Label l_data_a; + + hypnoinfo_t(); + + void update_data(const hypnodata_ptr_t& hd); + }; + + } +} + +#endif /* __N_WIDGETS_H */ -- cgit v0.9.0.2