comparison src/log.c @ 8898:de87e510ff9a

[gaim-migrate @ 9667] This makes the history plugin work in chats and not just conversations. To do this I had to change some functions in log.c to pass around the GaimLogType (GAIM_LOG_IM, GAIM_LOG_CHAT, or GAIM_LOG_SYSTEM). I hope that's not a problem... Here's how I see it: When creating a new GaimLog you need 3 things, the type, your account and the name of the other person/chat. It only makes sense that you would need those same 3 things to find a log. Or to calculate log size. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Fri, 07 May 2004 02:30:02 +0000
parents 4aee5a47937d
children db95a6641ec1
comparison
equal deleted inserted replaced
8897:8ffd1679df93 8898:de87e510ff9a
130 { 130 {
131 g_free(lu->name); 131 g_free(lu->name);
132 g_free(lu); 132 g_free(lu);
133 } 133 }
134 134
135 int gaim_log_get_total_size(const char *name, GaimAccount *account) 135 int gaim_log_get_total_size(GaimLogType type, const char *name, GaimAccount *account)
136 { 136 {
137 int size; 137 int size;
138 GSList *n; 138 GSList *n;
139 struct _gaim_logsize_user *lu; 139 struct _gaim_logsize_user *lu;
140 140
148 } else { 148 } else {
149 for (n = loggers; n; n = n->next) { 149 for (n = loggers; n; n = n->next) {
150 GaimLogLogger *logger = n->data; 150 GaimLogLogger *logger = n->data;
151 151
152 if(logger->total_size){ 152 if(logger->total_size){
153 size += (logger->total_size)(name, account); 153 size += (logger->total_size)(type, name, account);
154 } else if(logger->list) { 154 } else if(logger->list) {
155 GList *logs = (logger->list)(name, account); 155 GList *logs = (logger->list)(type, name, account);
156 int this_size = 0; 156 int this_size = 0;
157 157
158 while (logs) { 158 while (logs) {
159 GList *logs2 = logs->next; 159 GList *logs2 = logs->next;
160 GaimLog *log = (GaimLog*)(logs->data); 160 GaimLog *log = (GaimLog*)(logs->data);
194 } 194 }
195 gaim_log_logger_set(&txt_logger); 195 gaim_log_logger_set(&txt_logger);
196 } 196 }
197 197
198 198
199 GaimLogLogger *gaim_log_logger_new(void(*create)(GaimLog *), 199 GaimLogLogger *gaim_log_logger_new(
200 void(*write)(GaimLog *, GaimMessageFlags, const char *, 200 void(*create)(GaimLog *),
201 time_t, const char *), 201 void(*write)(GaimLog *, GaimMessageFlags, const char *, time_t, const char *),
202 void(*finalize)(GaimLog *), GList*(*list)(const char*, GaimAccount*), 202 void(*finalize)(GaimLog *),
203 char*(*read)(GaimLog*, GaimLogReadFlags*), 203 GList*(*list)(GaimLogType type, const char*, GaimAccount*),
204 int(*size)(GaimLog*)) 204 char*(*read)(GaimLog*, GaimLogReadFlags*),
205 int(*size)(GaimLog*))
205 { 206 {
206 GaimLogLogger *logger = g_new0(GaimLogLogger, 1); 207 GaimLogLogger *logger = g_new0(GaimLogLogger, 1);
207 logger->create = create; 208 logger->create = create;
208 logger->write = write; 209 logger->write = write;
209 logger->finalize = finalize; 210 logger->finalize = finalize;
266 const GaimLog *b = z; 267 const GaimLog *b = z;
267 268
268 return b->time - a->time; 269 return b->time - a->time;
269 } 270 }
270 271
271 GList *gaim_log_get_logs(const char *name, GaimAccount *account) 272 GList *gaim_log_get_logs(GaimLogType type, const char *name, GaimAccount *account)
272 { 273 {
273 GList *logs = NULL; 274 GList *logs = NULL;
274 GSList *n; 275 GSList *n;
275 for (n = loggers; n; n = n->next) { 276 for (n = loggers; n; n = n->next) {
276 GaimLogLogger *logger = n->data; 277 GaimLogLogger *logger = n->data;
277 if (!logger->list) 278 if (!logger->list)
278 continue; 279 continue;
279 logs = g_list_concat(logs, logger->list(name, account)); 280 logs = g_list_concat(logs, logger->list(type, name, account));
280 } 281 }
281 282
282 return g_list_sort(logs, gaim_log_compare); 283 return g_list_sort(logs, gaim_log_compare);
283 } 284 }
284 285
327 struct generic_logger_data { 328 struct generic_logger_data {
328 char *path; 329 char *path;
329 FILE *file; 330 FILE *file;
330 }; 331 };
331 332
332 static GList *log_lister_common(const char *screenname, GaimAccount *account, const char *ext, GaimLogLogger *logger) 333 static GList *log_lister_common(GaimLogType type, const char *name, GaimAccount *account, const char *ext, GaimLogLogger *logger)
333 { 334 {
334 GDir *dir; 335 GDir *dir;
335 GList *list = NULL; 336 GList *list = NULL;
336 const char *filename; 337 const char *filename;
337 char *me; 338 char *me;
338
339 const char *prpl; 339 const char *prpl;
340 char *path; 340 char *path;
341 341
342 if(!account) 342 if(!account)
343 return NULL; 343 return NULL;
344 344
345 me = g_strdup(gaim_normalize(account, gaim_account_get_username(account))); 345 if (type == GAIM_LOG_CHAT)
346 me = g_strdup_printf("%s.chat", gaim_normalize(account, gaim_account_get_username(account)));
347 else
348 me = g_strdup(gaim_normalize(account, gaim_account_get_username(account)));
346 349
347 /* does this seem like a bad way to get this component of the path to anyone else? --Nathan */ 350 /* does this seem like a bad way to get this component of the path to anyone else? --Nathan */
348 prpl = GAIM_PLUGIN_PROTOCOL_INFO 351 prpl = GAIM_PLUGIN_PROTOCOL_INFO
349 (gaim_find_prpl(gaim_account_get_protocol_id(account)))->list_icon(account, NULL); 352 (gaim_find_prpl(gaim_account_get_protocol_id(account)))->list_icon(account, NULL);
350 path = g_build_filename(gaim_user_dir(), "logs", prpl, me, gaim_normalize(account, screenname), NULL); 353 path = g_build_filename(gaim_user_dir(), "logs", prpl, me, gaim_normalize(account, name), NULL);
351 g_free(me); 354 g_free(me);
352 355
353 if (!(dir = g_dir_open(path, 0, NULL))) { 356 if (!(dir = g_dir_open(path, 0, NULL))) {
354 g_free(path); 357 g_free(path);
355 return NULL; 358 return NULL;
356 } 359 }
360
357 while ((filename = g_dir_read_name(dir))) { 361 while ((filename = g_dir_read_name(dir))) {
358 if (gaim_str_has_suffix(filename, ext) && 362 if (gaim_str_has_suffix(filename, ext) &&
359 strlen(filename) == 17 + strlen(ext)) { 363 strlen(filename) == 17 + strlen(ext)) {
360 GaimLog *log; 364 GaimLog *log;
361 struct generic_logger_data *data; 365 struct generic_logger_data *data;
362 time_t stamp = gaim_str_to_time(filename, FALSE); 366 time_t stamp = gaim_str_to_time(filename, FALSE);
363 367
364 log = gaim_log_new(GAIM_LOG_IM, screenname, account, stamp); 368 log = gaim_log_new(type, name, account, stamp);
365 log->logger = logger; 369 log->logger = logger;
366 log->logger_data = data = g_new0(struct generic_logger_data, 1); 370 log->logger_data = data = g_new0(struct generic_logger_data, 1);
367 data->path = g_build_filename(path, filename, NULL); 371 data->path = g_build_filename(path, filename, NULL);
368 list = g_list_append(list, log); 372 list = g_list_append(list, log);
369 } 373 }
412 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); 416 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account)));
413 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO 417 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO
414 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL); 418 (gaim_find_prpl(gaim_account_get_protocol(log->account)))->list_icon(log->account, NULL);
415 char *dir; 419 char *dir;
416 FILE *file; 420 FILE *file;
421
422 if (log->type == GAIM_LOG_CHAT) {
423 char *chat = g_strdup_printf("%s.chat", guy);
424 g_free(guy);
425 guy = chat;
426 }
417 427
418 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time)); 428 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time));
419 429
420 dir = g_build_filename(ud, "logs", 430 dir = g_build_filename(ud, "logs",
421 prpl, guy, gaim_normalize(log->account, log->name), NULL); 431 prpl, guy, gaim_normalize(log->account, log->name), NULL);
465 fclose(log->logger_data); 475 fclose(log->logger_data);
466 log->logger_data = NULL; 476 log->logger_data = NULL;
467 } 477 }
468 } 478 }
469 479
470 static GList *xml_logger_list(const char *sn, GaimAccount *account) 480 static GList *xml_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
471 { 481 {
472 return log_lister_common(sn, account, ".xml", &xml_logger); 482 return log_lister_common(type, sn, account, ".xml", &xml_logger);
473 } 483 }
474 484
475 static GaimLogLogger xml_logger = { 485 static GaimLogLogger xml_logger = {
476 N_("XML"), "xml", 486 N_("XML"), "xml",
477 NULL, 487 NULL,
593 g_free(data->path); 603 g_free(data->path);
594 g_free(data); 604 g_free(data);
595 } 605 }
596 } 606 }
597 607
598 static GList *html_logger_list(const char *sn, GaimAccount *account) 608 static GList *html_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
599 { 609 {
600 return log_lister_common(sn, account, ".html", &html_logger); 610 return log_lister_common(type, sn, account, ".html", &html_logger);
601 } 611 }
602 612
603 static GList *html_logger_list_syslog(GaimAccount *account) 613 static GList *html_logger_list_syslog(GaimAccount *account)
604 { 614 {
605 return log_lister_common(".system", account, ".html", &html_logger); 615 return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".html", &html_logger);
606 } 616 }
607 617
608 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags) 618 static char *html_logger_read(GaimLog *log, GaimLogReadFlags *flags)
609 { 619 {
610 char *read, *minus_header; 620 char *read, *minus_header;
686 if (!data) { 696 if (!data) {
687 /* This log is new. We could use the loggers 'new' function, but 697 /* This log is new. We could use the loggers 'new' function, but
688 * creating a new file there would result in empty files in the case 698 * creating a new file there would result in empty files in the case
689 * that you open a convo with someone, but don't say anything. 699 * that you open a convo with someone, but don't say anything.
690 * 700 *
691 * The log is also not system log. Because if it is, data would be 701 * The log is also not a system log. Because if it is, data would
692 * created in txt_logger_create 702 * be created in txt_logger_create
693 */ 703 */
694 char *ud = gaim_user_dir(); 704 char *ud = gaim_user_dir();
695 char *filename;
696 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account))); 705 char *guy = g_strdup(gaim_normalize(log->account, gaim_account_get_username(log->account)));
697 char *chat; 706 char *chat;
698 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO 707 const char *prpl = GAIM_PLUGIN_PROTOCOL_INFO
699 (gaim_find_prpl(gaim_account_get_protocol_id(log->account)))->list_icon(log->account, NULL); 708 (gaim_find_prpl(gaim_account_get_protocol_id(log->account)))->list_icon(log->account, NULL);
700 char *dir; 709 char *dir;
710 char *filename;
701 711
702 if (log->type == GAIM_LOG_CHAT) { 712 if (log->type == GAIM_LOG_CHAT) {
703 chat = g_strdup_printf("%s.chat", guy); 713 chat = g_strdup_printf("%s.chat", guy);
704 g_free(guy); 714 g_free(guy);
705 guy = chat; 715 guy = chat;
706 } 716 }
717
707 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time)); 718 strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time));
708 719
709 dir = g_build_filename(ud, "logs", 720 dir = g_build_filename(ud, "logs",
710 prpl, guy, gaim_normalize(log->account, log->name), NULL); 721 prpl, guy, gaim_normalize(log->account, log->name), NULL);
711 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR); 722 gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
779 g_free(data->path); 790 g_free(data->path);
780 g_free(data); 791 g_free(data);
781 } 792 }
782 } 793 }
783 794
784 static GList *txt_logger_list(const char *sn, GaimAccount *account) 795 static GList *txt_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
785 { 796 {
786 return log_lister_common(sn, account, ".txt", &txt_logger); 797 return log_lister_common(type, sn, account, ".txt", &txt_logger);
787 } 798 }
788 799
789 static GList *txt_logger_list_syslog(GaimAccount *account) 800 static GList *txt_logger_list_syslog(GaimAccount *account)
790 { 801 {
791 return log_lister_common(".system", account, ".txt", &txt_logger); 802 return log_lister_common(GAIM_LOG_SYSTEM, ".system", account, ".txt", &txt_logger);
792 } 803 }
793 804
794 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags) 805 static char *txt_logger_read(GaimLog *log, GaimLogReadFlags *flags)
795 { 806 {
796 char *read, *minus_header, *minus_header2; 807 char *read, *minus_header, *minus_header2;
865 GaimStringref *pathref; 876 GaimStringref *pathref;
866 int offset; 877 int offset;
867 int length; 878 int length;
868 }; 879 };
869 880
870 static GList *old_logger_list(const char *sn, GaimAccount *account) 881 static GList *old_logger_list(GaimLogType type, const char *sn, GaimAccount *account)
871 { 882 {
872 FILE *file; 883 FILE *file;
873 char buf[BUF_LONG]; 884 char buf[BUF_LONG];
874 struct tm tm; 885 struct tm tm;
875 char month[4]; 886 char month[4];
991 gaim_stringref_unref(pathref); 1002 gaim_stringref_unref(pathref);
992 fclose(file); 1003 fclose(file);
993 return list; 1004 return list;
994 } 1005 }
995 1006
996 static int old_logger_total_size(const char *name, GaimAccount *account) 1007 static int old_logger_total_size(GaimLogType type, const char *name, GaimAccount *account)
997 { 1008 {
998 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name)); 1009 char *logfile = g_strdup_printf("%s.log", gaim_normalize(account, name));
999 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL); 1010 char *pathstr = g_build_filename(gaim_user_dir(), "logs", logfile, NULL);
1000 int size; 1011 int size;
1001 struct stat st; 1012 struct stat st;