Changeset 9290
- Timestamp:
- Oct 11, 2009, 6:23:31 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gtk/tr-core.c
r9130 r9290 459 459 } 460 460 461 static int462 tr_strcmp( const void * a,463 const void * b )464 {465 if( a && b ) return strcmp( a, b );466 if( a ) return 1;467 if( b ) return -1;468 return 0;469 }470 471 461 #ifdef HAVE_GIO 462 463 struct watchdir_file 464 { 465 char * filename; 466 time_t mtime; 467 }; 468 469 static int 470 compare_watchdir_file_to_filename( const void * a, const void * filename ) 471 { 472 return strcmp( ((const struct watchdir_file*)a)->filename, filename ); 473 } 474 475 static void 476 watchdir_file_update_mtime( struct watchdir_file * file ) 477 { 478 GFile * gfile = g_file_new_for_path( file->filename ); 479 GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL ); 480 481 file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED ); 482 483 g_object_unref( G_OBJECT( info ) ); 484 g_object_unref( G_OBJECT( gfile ) ); 485 } 486 487 static struct watchdir_file* 488 watchdir_file_new( const char * filename ) 489 { 490 struct watchdir_file * f; 491 492 f = g_new( struct watchdir_file, 1 ); 493 f->filename = g_strdup( filename ); 494 watchdir_file_update_mtime( f ); 495 496 return f; 497 } 498 499 static void 500 watchdir_file_free( struct watchdir_file * f ) 501 { 502 g_free( f->filename ); 503 g_free( f ); 504 } 505 472 506 static gboolean 473 507 watchFolderIdle( gpointer gcore ) 474 508 { 509 GSList * l; 510 GSList * addme = NULL; 511 GSList * monitor_files = NULL; 475 512 TrCore * core = TR_CORE( gcore ); 476 513 const time_t now = time( NULL ); 514 struct TrCorePrivate * p = core->priv; 515 516 /* of the monitor_files, make a list of those that haven't 517 * changed lately, since they should be ready to add */ 518 for( l=p->monitor_files; l!=NULL; l=l->next ) { 519 struct watchdir_file * f = l->data; 520 watchdir_file_update_mtime( f ); 521 if( f->mtime + 2 >= now ) 522 monitor_files = g_slist_prepend( monitor_files, f ); 523 else { 524 addme = g_slist_prepend( addme, g_strdup( f->filename ) ); 525 watchdir_file_free( f ); 526 } 527 } 528 529 /* add the torrents from that list */ 477 530 core->priv->adding_from_watch_dir = TRUE; 478 tr_core_add_list_defaults( core, core->priv->monitor_files, TRUE );531 tr_core_add_list_defaults( core, addme, TRUE ); 479 532 core->priv->adding_from_watch_dir = FALSE; 480 533 481 /* cleanup */ 482 core->priv->monitor_files = NULL; 534 /* update the monitor_files list */ 535 g_slist_free( p->monitor_files ); 536 p->monitor_files = monitor_files; 537 538 /* if monitor_files is nonempty, keep checking every second */ 539 if( core->priv->monitor_files ) 540 return TRUE; 483 541 core->priv->monitor_idle_tag = 0; 484 542 return FALSE; 485 } 486 487 static void 488 maybeAddTorrent( TrCore * core, 489 543 544 } 545 546 static void 547 maybeAddTorrent( TrCore * core, const char * filename ) 490 548 { 491 549 const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" ); … … 495 553 struct TrCorePrivate * p = core->priv; 496 554 497 if( !g_slist_find_custom( p->monitor_files, filename, 498 (GCompareFunc)strcmp ) ) 499 p->monitor_files = 500 g_slist_append( p->monitor_files, g_strdup( filename ) ); 555 if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) ) 556 p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) ); 557 501 558 if( !p->monitor_idle_tag ) 502 559 p->monitor_idle_tag = gtr_timeout_add_seconds( 1, watchFolderIdle, core ); … … 539 596 g_dir_close( dir ); 540 597 } 598 } 599 600 static int 601 tr_strcmp( const void * a, 602 const void * b ) 603 { 604 if( a && b ) return strcmp( a, b ); 605 if( a ) return 1; 606 if( b ) return -1; 607 return 0; 541 608 } 542 609
Note: See TracChangeset
for help on using the changeset viewer.