From 2f63794da7acbfcf61558428cd84f590a73fc13f Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Sun, 6 Sep 2015 15:10:51 -0600 Subject: revert to original time --- CMakeLists.txt | 2 +- logging.cpp | 8 +++-- logging.h | 2 +- main.cpp | 11 ++++--- pomodori.h | 10 ++++++ totimer.cpp | 37 ++++++++++++++++++---- totimer.h | 6 +++- trayicon.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trayicon.h | 14 +++++++++ 9 files changed, 171 insertions(+), 16 deletions(-) create mode 100644 pomodori.h create mode 100644 trayicon.cpp create mode 100644 trayicon.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ea48c46..886d20c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ link_directories(${GTKLIB_LIBRARY_DIRS}) set(CMAKE_CXX_FLAGS " -std=c++11 -lnotify -lsqlite3") -set(SOURCE_FILES main.cpp totimer.cpp totimer.h logging.cpp logging.h) +set(SOURCE_FILES main.cpp totimer.cpp totimer.h logging.cpp logging.h trayicon.cpp trayicon.h pomodori.h) add_executable(pomodori ${SOURCE_FILES}) target_link_libraries(pomodori ${GTKLIB_LIBRARIES}) \ No newline at end of file diff --git a/logging.cpp b/logging.cpp index f9b3100..798f00f 100644 --- a/logging.cpp +++ b/logging.cpp @@ -10,6 +10,8 @@ #include #include #include +#include "pomodori.h" +#include "trayicon.h" struct passwd *pw = getpwuid(getuid()); @@ -21,7 +23,7 @@ void log(tres* res,int code) sqlite3 *db; char *zErrMsg = 0; char sqlop[50]; - sprintf(sqlop,"%s/.pomodori",pw->pw_dir); + sprintf(sqlop,"%s/." APPNAME,pw->pw_dir); int rc = sqlite3_open(sqlop,&db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); @@ -36,5 +38,7 @@ void log(tres* res,int code) } sqlite3_close(db); } - exit(0); + delete res->reason; + delete res; + tray_deactivate(); } \ No newline at end of file diff --git a/logging.h b/logging.h index 1f655dd..1cfa693 100644 --- a/logging.h +++ b/logging.h @@ -9,7 +9,7 @@ typedef struct tres { guint32 time; - const char* reason; + char* reason; } tres; void log(tres* res,int code); diff --git a/main.cpp b/main.cpp index b5ddb6f..8865c33 100644 --- a/main.cpp +++ b/main.cpp @@ -3,17 +3,18 @@ #include #include "logging.h" #include "totimer.h" +#include "pomodori.h" +#include "trayicon.h" +#include "string.h" + int main(int argc, char** argv) { if (argc<2) return 0; gtk_init(&argc, &argv); - notify_init("pomodori"); - tres lastres; - lastres.time=0; - lastres.reason=argv[1]; - settimer(&lastres,POTIME); + timer_init(); + tray_init(); gtk_main(); return 0; } \ No newline at end of file diff --git a/pomodori.h b/pomodori.h new file mode 100644 index 0000000..df4e937 --- /dev/null +++ b/pomodori.h @@ -0,0 +1,10 @@ +// +// Created by joe on 9/6/15. +// + +#ifndef POMODORI_POMODORI_H +#define POMODORI_POMODORI_H + +#define APPNAME "Pomodori" + +#endif //POMODORI_POMODORI_H diff --git a/totimer.cpp b/totimer.cpp index 05e2a01..ec9b5dd 100644 --- a/totimer.cpp +++ b/totimer.cpp @@ -4,6 +4,21 @@ #include "totimer.h" #include +#include "pomodori.h" +#include "trayicon.h" + +bool timeout; +guint timerid; + +void timer_kill() +{ + if (timeout) + { + g_source_remove(timerid); + timeout=false; + tray_deactivate(); + } +} void quit(NotifyNotification *note,gpointer user_data) { @@ -16,7 +31,7 @@ void timeup(NotifyNotification *note,const char *action,gpointer user_data){ tres* lastres = (tres*)user_data; switch (action[0]){ case 'M': - settimer(lastres, POTEXT); + timer_set(lastres, POTEXT); break; case 'G': log(lastres,1); @@ -30,6 +45,7 @@ void timeup(NotifyNotification *note,const char *action,gpointer user_data){ bool notify(gpointer user_data) { + timeout=false; tres* lastres = (tres*)user_data; NotifyNotification *n; char str[20]; @@ -37,19 +53,28 @@ bool notify(gpointer user_data) sprintf(str,"Time's up!(%d)",lastres->time-POTIME); else sprintf(str,"Time's up!"); - n = notify_notification_new ("Pomodori",str, NULL); + n = notify_notification_new (APPNAME,str, NULL); notify_notification_set_urgency(n,NOTIFY_URGENCY_CRITICAL); notify_notification_add_action(n,"M", "More",(NotifyActionCallback)timeup,lastres,NULL); notify_notification_add_action(n,"G", "Good",(NotifyActionCallback)timeup,lastres,NULL); g_signal_connect (n, "closed", G_CALLBACK(quit), NULL); -// printf("1"); notify_notification_set_timeout(n,0); //3 seconds notify_notification_show(n,NULL); return FALSE; } -int settimer(tres* lastres, guint32 wait) +void timer_set(tres* lastres, guint32 wait) +{ + if (!timeout) + { + timeout=true; + lastres->time = lastres->time + wait; + timerid = g_timeout_add(wait*60*1000,(GSourceFunc)notify,lastres); + } +} + +void timer_init() { - lastres->time = lastres->time + wait; - g_timeout_add(wait*60*1000,(GSourceFunc)notify,lastres); + timeout=false; + notify_init(APPNAME); } \ No newline at end of file diff --git a/totimer.h b/totimer.h index ac8f1ef..0bbd5bb 100644 --- a/totimer.h +++ b/totimer.h @@ -12,8 +12,12 @@ #define POTIME 25 #define POTEXT 5 +void timer_init(); + bool notify(gpointer user_data); -int settimer(tres* lastres,guint32 wait); +void timer_set(tres* lastres,guint32 wait); + +void timer_kill(); #endif //POMODORI_TOTIMER_H diff --git a/trayicon.cpp b/trayicon.cpp new file mode 100644 index 0000000..e434352 --- /dev/null +++ b/trayicon.cpp @@ -0,0 +1,97 @@ +// +// Created by joe on 9/6/15. +// + +#include "trayicon.h" +#include +#include +#include +#include "pomodori.h" +#include "logging.h" +#include "totimer.h" + +#define FREESYM "/usr/share/icons/gnome/scalable/actions/view-list-symbolic.svg" +#define BUSYSYM "/usr/share/icons/gnome/scalable/actions/view-dual-symbolic.svg" + +const int objn = 4; +const char* objs[] = {"Paper","Study","Homework","Project"}; + +GtkStatusIcon *tray; +GtkWidget *tray_menu; + +void menucall(GtkMenuItem *menuitem,gpointer user_data) +{ + tres *lastres = new tres; + lastres->time=0; + lastres->reason=new char[10]; + strcpy(lastres->reason,(char *)user_data); + timer_set(lastres,POTIME); + tray_activate((char *)user_data); +} + +void on_right (GtkStatusIcon *status_icon,guint button,guint activate_time,gpointer user_data){ + gtk_menu_popup(GTK_MENU(tray_menu),NULL,NULL,NULL,NULL,button,activate_time); +} + +void closeall(GtkMenuItem *menuitem,gpointer user_data) +{ + exit(0); +} + +void stop(GtkMenuItem *menuitem,gpointer user_data) +{ + timer_kill(); +} + +void starttimer(const char* reason); + +void tray_init() +{ + tray = gtk_status_icon_new(); + tray_deactivate(); + gtk_status_icon_set_visible(tray, TRUE); + + tray_menu = gtk_menu_new (); /* Don't need to show menus */ + + /* Create the menu items */ + GtkWidget *items[objn]; + for (int i=0;i