about summary refs log tree commit diff stats
path: root/src/chat_log.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-10-14 16:45:39 +0100
committerJames Booth <boothj5@gmail.com>2012-10-14 16:45:39 +0100
commit31295005283ecc1aee7dbc72c90c915b8c71c090 (patch)
treeef7b45532535d8439222660c2c9c62f7b071d98c /src/chat_log.c
parent460b244048d9002b9fa9f3acd82998492cbfc93d (diff)
downloadprofani-tty-31295005283ecc1aee7dbc72c90c915b8c71c090.tar.gz
Show log if session started today
Diffstat (limited to 'src/chat_log.c')
-rw-r--r--src/chat_log.c59
1 files changed, 35 insertions, 24 deletions
diff --git a/src/chat_log.c b/src/chat_log.c
index 45daaeec..2a68c547 100644
--- a/src/chat_log.c
+++ b/src/chat_log.c
@@ -33,7 +33,7 @@
 #include "ui.h"
 
 static GHashTable *logs;
-static GDateTime *started;
+static GDateTime *session_started;
 
 struct dated_chat_log {
     gchar *filename;
@@ -50,7 +50,7 @@ static char * _get_log_filename(char *other, const char * const login,
 void
 chat_log_init(void)
 {   
-    started = g_date_time_new_now_local();
+    session_started = g_date_time_new_now_local();
     log_info("Initialising chat logs");
     logs = g_hash_table_new_full(g_str_hash, (GEqualFunc) _key_equals, g_free, 
         (GDestroyNotify)_free_chat_log);
@@ -98,36 +98,47 @@ chat_log_get_previous(const gchar * const login, gchar *recipient,
     GSList *history)
 {
     GDateTime *now = g_date_time_new_now_local();
-    char *filename = _get_log_filename(recipient, login, now);
-    
-    FILE *logp = fopen(filename, "r");
-    char *line = NULL;
-    size_t read = 0;
-    if (logp != NULL) {
-        size_t length = getline(&line, &read, logp);
-        while (length != -1) {
-             char *copy = malloc(length);
-             copy = strncpy(copy, line, length);
-             copy[length -1] = '\0';
-             history = g_slist_append(history, copy);
-             free(line);
-             line = NULL;
-             read = 0;
-             length = getline(&line, &read, logp);
+    gint session_started_day = g_date_time_get_day_of_year(session_started);
+    gint day_now = g_date_time_get_day_of_year(now);
+
+    // session started today
+    if (day_now == session_started_day) {
+        char *filename = _get_log_filename(recipient, login, now);
+        
+        FILE *logp = fopen(filename, "r");
+        char *line = NULL;
+        size_t read = 0;
+        if (logp != NULL) {
+            size_t length = getline(&line, &read, logp);
+            while (length != -1) {
+                 char *copy = malloc(length);
+                 copy = strncpy(copy, line, length);
+                 copy[length -1] = '\0';
+                 history = g_slist_append(history, copy);
+                 free(line);
+                 line = NULL;
+                 read = 0;
+                 length = getline(&line, &read, logp);
+            }
         }
-    }
+        
+        free(filename);
+        g_date_time_unref(now);
+        
+        return history;
     
-    free(filename);
-    g_date_time_unref(now);
-    
-    return history;
+    // session started before today
+    } else {
+        
+        return NULL;
+    }
 }
 
 void
 chat_log_close(void)
 {
     g_hash_table_remove_all(logs);
-    g_date_time_unref(started);
+    g_date_time_unref(session_started);
 }
 
 static struct dated_chat_log *