summaryrefslogtreecommitdiff
path: root/totimer.cpp
blob: ec9b5dd1bbc312197230c9ea161537dec5aea2a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Created by joe on 9/3/15.
//

#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)
{
    tres* lastres = (tres*)user_data;
    log(lastres,0);
}

void timeup(NotifyNotification *note,const char *action,gpointer user_data){
    g_object_unref(G_OBJECT(note));
    tres* lastres = (tres*)user_data;
    switch (action[0]){
        case 'M':
            timer_set(lastres, POTEXT);
            break;
        case 'G':
            log(lastres,1);
            break;
        case 'F':
        default:
            log(lastres,0);
            break;
    }
}

bool notify(gpointer user_data)
{
    timeout=false;
    tres* lastres = (tres*)user_data;
    NotifyNotification *n;
    char str[20];
    if (lastres->time>POTIME)
        sprintf(str,"Time's up!(%d)",lastres->time-POTIME);
    else
        sprintf(str,"Time's up!");
    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);
    notify_notification_set_timeout(n,0); //3 seconds
    notify_notification_show(n,NULL);
    return FALSE;
}

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()
{
    timeout=false;
    notify_init(APPNAME);
}