summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rw-r--r--logging.cpp8
-rw-r--r--logging.h2
-rw-r--r--main.cpp11
-rw-r--r--pomodori.h10
-rw-r--r--totimer.cpp37
-rw-r--r--totimer.h6
-rw-r--r--trayicon.cpp97
-rw-r--r--trayicon.h14
9 files changed, 171 insertions, 16 deletions
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 <sqlite3.h>
#include <unistd.h>
#include <pwd.h>
+#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 <gtk/gtk.h>
#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<stdio.h>
+#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 <gtk/gtk.h>
+#include <stdlib.h>
+#include <string.h>
+#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<objn;++i)
+ {
+ items[i]=gtk_menu_item_new_with_label (objs[i]);
+ gtk_menu_shell_append(GTK_MENU_SHELL (tray_menu), items[i]);
+ g_signal_connect (items[i], "activate", G_CALLBACK(menucall), gpointer(objs[i]));
+ gtk_widget_show (items[i]);
+ }
+ GtkWidget *stop_item = gtk_menu_item_new_with_label ("Stop");
+ GtkWidget *exit_item = gtk_menu_item_new_with_label ("Exit");
+ GtkWidget *sep_item = gtk_separator_menu_item_new();
+
+ gtk_menu_shell_append(GTK_MENU_SHELL (tray_menu), sep_item);
+ gtk_menu_shell_append(GTK_MENU_SHELL (tray_menu), stop_item);
+ gtk_menu_shell_append(GTK_MENU_SHELL (tray_menu), exit_item);
+
+ /* Attach the callback functions to the activate signal */
+ g_signal_connect (stop_item, "activate", G_CALLBACK(stop), NULL);
+ g_signal_connect (exit_item, "activate", G_CALLBACK(closeall), NULL);
+
+ /* We do need to show menu items */
+ gtk_widget_show (sep_item);
+ gtk_widget_show (stop_item);
+ gtk_widget_show (exit_item);
+
+ g_signal_connect (tray, "popup-menu", G_CALLBACK(on_right), NULL);
+}
+
+void tray_activate(char* info)
+{
+ gtk_status_icon_set_from_file(tray,BUSYSYM);
+ char inf[30];
+ sprintf(inf,APPNAME " - %s",info);
+ gtk_status_icon_set_tooltip_text(tray, inf);
+}
+
+void tray_deactivate()
+{
+ gtk_status_icon_set_from_file(tray,FREESYM);
+ gtk_status_icon_set_tooltip_text(tray, APPNAME);
+} \ No newline at end of file
diff --git a/trayicon.h b/trayicon.h
new file mode 100644
index 0000000..e8134ba
--- /dev/null
+++ b/trayicon.h
@@ -0,0 +1,14 @@
+//
+// Created by joe on 9/6/15.
+//
+
+#ifndef POMODORI_TRAYICON_H
+#define POMODORI_TRAYICON_H
+
+void tray_init();
+
+void tray_activate(char* info);
+
+void tray_deactivate();
+
+#endif //POMODORI_TRAYICON_H