comparison src/filedata.c @ 791:6d65167764ea

realtime file monitor
author nadvornik
date Fri, 06 Jun 2008 21:50:09 +0000
parents 436674261840
children 99ea3d973ad3
comparison
equal deleted inserted replaced
790:436674261840 791:6d65167764ea
1441 typedef struct _NotifyData NotifyData; 1441 typedef struct _NotifyData NotifyData;
1442 1442
1443 struct _NotifyData { 1443 struct _NotifyData {
1444 FileDataNotifyFunc func; 1444 FileDataNotifyFunc func;
1445 gpointer data; 1445 gpointer data;
1446 NotifyPriority priority;
1446 }; 1447 };
1447 1448
1448 static GList *notify_func_list = NULL; 1449 static GList *notify_func_list = NULL;
1449 1450
1450 gint file_data_register_notify_func(FileDataNotifyFunc func, gpointer data) 1451 static gint file_data_notify_sort(gconstpointer a, gconstpointer b)
1452 {
1453 NotifyData *nda = (NotifyData *)a;
1454 NotifyData *ndb = (NotifyData *)b;
1455 if (nda->priority < ndb->priority) return -1;
1456 if (nda->priority > ndb->priority) return 1;
1457 return 0;
1458 }
1459
1460 gint file_data_register_notify_func(FileDataNotifyFunc func, gpointer data, NotifyPriority priority)
1451 { 1461 {
1452 NotifyData *nd = g_new(NotifyData, 1); 1462 NotifyData *nd = g_new(NotifyData, 1);
1453 nd->func = func; 1463 nd->func = func;
1454 nd->data = data; 1464 nd->data = data;
1455 notify_func_list = g_list_prepend(notify_func_list, nd); 1465 nd->priority = priority;
1456 DEBUG_1("Notify func registered: %p\n", nd); 1466 notify_func_list = g_list_insert_sorted(notify_func_list, nd, file_data_notify_sort);
1467 DEBUG_1("Notify func registered: %p", nd);
1457 return TRUE; 1468 return TRUE;
1458 } 1469 }
1459 1470
1460 gint file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data) 1471 gint file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data)
1461 { 1472 {
1466 NotifyData *nd = (NotifyData *)work->data; 1477 NotifyData *nd = (NotifyData *)work->data;
1467 if (nd->func == func && nd->data == data) 1478 if (nd->func == func && nd->data == data)
1468 { 1479 {
1469 notify_func_list = g_list_delete_link(notify_func_list, work); 1480 notify_func_list = g_list_delete_link(notify_func_list, work);
1470 g_free(nd); 1481 g_free(nd);
1471 DEBUG_1("Notify func unregistered: %p\n", nd); 1482 DEBUG_1("Notify func unregistered: %p", nd);
1472 return TRUE; 1483 return TRUE;
1473 } 1484 }
1474 work = work->next; 1485 work = work->next;
1475 } 1486 }
1476 return FALSE; 1487 return FALSE;
1481 { 1492 {
1482 GList *work = notify_func_list; 1493 GList *work = notify_func_list;
1483 while(work) 1494 while(work)
1484 { 1495 {
1485 NotifyData *nd = (NotifyData *)work->data; 1496 NotifyData *nd = (NotifyData *)work->data;
1486 DEBUG_1("Notify func calling: %p %s\n", nd, fd->path); 1497 DEBUG_1("Notify func calling: %p %s", nd, fd->path);
1487 nd->func(fd, nd->data); 1498 nd->func(fd, nd->data);
1488 work = work->next; 1499 work = work->next;
1489 } 1500 }
1490 } 1501 }
1491 1502
1492 void file_data_sc_send_notification(FileData *fd) 1503 void file_data_sc_send_notification(FileData *fd)
1493 { 1504 {
1494 } 1505 }
1495 1506
1496 1507
1497 1508 static GHashTable *file_data_monitor_pool = NULL;
1498 1509 static gint realtime_monitor_id = -1;
1499 1510
1500 1511 static void realtime_monitor_check_cb(gpointer key, gpointer value, gpointer data)
1501 1512 {
1502 1513 FileData *fd = key;
1503 1514 struct stat st;
1515
1516 stat_utf8(fd->path, &st);
1517
1518 file_data_check_changed_files(fd, &st);
1519
1520 DEBUG_1("monitor %s", fd->path);
1521 }
1522
1523 static gboolean realtime_monitor_cb(gpointer data)
1524 {
1525 g_hash_table_foreach(file_data_monitor_pool, realtime_monitor_check_cb, NULL);
1526 return TRUE;
1527 }
1528
1529 gint file_data_register_real_time_monitor(FileData *fd)
1530 {
1531 gint count = 0;
1532
1533 file_data_ref(fd);
1534
1535 if (!file_data_monitor_pool)
1536 file_data_monitor_pool = g_hash_table_new(g_direct_hash, g_direct_equal);
1537
1538 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd));
1539
1540 DEBUG_1("Register realtime %d %s", count, fd->path);
1541
1542 count++;
1543 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count));
1544
1545 if (realtime_monitor_id == -1)
1546 {
1547 realtime_monitor_id = g_timeout_add(5000, realtime_monitor_cb, NULL);
1548 }
1549 return TRUE;
1550 }
1551
1552 gint file_data_unregister_real_time_monitor(FileData *fd)
1553 {
1554 gint count;
1555 g_assert(file_data_monitor_pool);
1556
1557 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd));
1558
1559 DEBUG_1("Unregister realtime %d %s", count, fd->path);
1560
1561 g_assert(count > 0);
1562
1563 count--;
1564
1565 if (count == 0)
1566 g_hash_table_remove(file_data_monitor_pool, fd);
1567 else
1568 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count));
1569
1570 file_data_unref(fd);
1571
1572 if (g_hash_table_size(file_data_monitor_pool) == 0)
1573 {
1574 g_source_remove(realtime_monitor_id);
1575 realtime_monitor_id = -1;
1576 return FALSE;
1577 }
1578 return TRUE;
1579 }
1580
1581
1582
1583
1584
1585
1586