Skip to content

Instantly share code, notes, and snippets.

@berkay-repos
Last active April 10, 2021 22:28
Show Gist options
  • Save berkay-repos/2a36ecd1a58dbd99bf178dbc944e603f to your computer and use it in GitHub Desktop.
Save berkay-repos/2a36ecd1a58dbd99bf178dbc944e603f to your computer and use it in GitHub Desktop.
Reminder
#include <libnotify/notify.h>
#include <time.h>
#define MAXLINE 1000
int cmp(char *, char *);
int detect_month(char[9], char[12][9]);
void cat(char *, char *);
void cpy(char *, char *);
int len(char *);
int get_line(const char *);
const char loc[75] = "/home/cenderme/.config/reminders";
const char *ploc = loc;
int main() {
char *p_line = (char *)malloc(MAXLINE * sizeof(char));
time_t tnow;
double diff_t;
struct tm *rem_time;
int day, month, year, hour, minute, dd, hh, mm;
int ups[100][3];
char mon[9];
char months[12][9] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
char msg[500], m_str[3], msg_data[MAXLINE] = ""; // error if not defined
while (1) {
FILE *fp = fopen(ploc, "r");
/* Go to the end of the header. */
fseek(fp, 59, SEEK_SET);
int n = get_line(ploc);
/* n is limited at 1 because of the header of the configuration file. */
while (n-- > 1) {
cpy(msg_data, "");
fscanf(fp, "%i %s %i %i:%i\t%i %i %i\t%[^\n]s", &day, mon, &year, &hour,
&minute, &dd, &hh, &mm, msg);
char *p_month = mon;
int month = detect_month(mon, months);
time(&tnow);
rem_time = localtime(&tnow);
rem_time->tm_year = year - 1900;
rem_time->tm_mon = month;
rem_time->tm_mday = day;
rem_time->tm_hour = hour;
rem_time->tm_min = minute;
rem_time->tm_sec = 0;
rem_time->tm_isdst = -1;
int ret = mktime(rem_time);
diff_t = difftime(ret, tnow);
double foo_diff = diff_t / 60 / 60 / 24;
if (foo_diff > 0) {
if ((int)foo_diff > 0) {
sprintf(m_str, "%i", (int)foo_diff);
cat(msg_data, m_str);
cat(msg_data, " days, ");
}
foo_diff = (foo_diff - (int)foo_diff) * 24;
if ((int)foo_diff > 0) {
sprintf(m_str, "%i", (int)foo_diff);
cat(msg_data, m_str);
cat(msg_data, " hours, ");
}
foo_diff = (foo_diff - (int)foo_diff) * 60;
if ((int)foo_diff > 0) {
sprintf(m_str, "%i", (int)foo_diff);
if (len(msg_data) > 0)
cat(msg_data, "and ");
cat(msg_data, m_str);
cat(msg_data, " minutes ");
}
cat(msg_data, "remaining...\n");
cat(msg_data, msg);
if (diff_t - mm * 60 - hh * 60 * 60 - dd * 60 * 60 * 24 < 0) {
notify_init("reminder_app");
NotifyNotification *foo = notify_notification_new(
asctime(rem_time), msg_data,
"/usr/share/icons/Adwaita/512x512/mimetypes/"
"x-office-calendar-symbolic.png");
notify_notification_show(foo, NULL);
g_object_unref(G_OBJECT(foo));
notify_uninit();
}
}
}
fclose(fp);
sleep(1800);
}
return 0;
}
int cmp(char *s, char *t) {
for (; *s == *t; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}
int detect_month(char data_month[9], char month_list[12][9]) {
for (int i = 0; i != 12; i++) {
int res = cmp(data_month, month_list[i]);
if (res == 0)
return i;
}
return 1;
}
void cpy(char *s, char *t) {
while ((*s++ = *t++))
;
}
int len(char *s) {
char *p = s;
while (*p++)
;
return --p - s;
/*
Alternative
int n = 0;
while (*s++)
n++;
return n;
*/
}
void cat(char *s, char *t) { cpy(s + len(s), t); }
int get_line(const char *s) {
FILE *fp = fopen(s, "r");
int n = 0, c;
while ((c = fgetc(fp)) != EOF)
if (c == '\n')
++n;
fclose(fp);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment