db.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <shlwapi.h>
  2. #include "api__ml_downloads.h"
  3. #include "Downloaded.h"
  4. #include "../nde/nde_c.h"
  5. #include "../nu/AutoLock.h"
  6. /* DB Schema
  7. Source
  8. Url
  9. Title
  10. DownloadDate
  11. Length
  12. Filename
  13. */
  14. static Nullsoft::Utility::LockGuard dbcs;
  15. static nde_table_t table = 0;
  16. static nde_database_t db = 0;
  17. using namespace Nullsoft::Utility;
  18. enum
  19. {
  20. DB_ID_SOURCE = 0,
  21. DB_ID_URL = 1,
  22. DB_ID_TITLE = 2,
  23. DB_ID_DOWNLOADDATE = 3,
  24. DB_ID_LENGTH = 4,
  25. DB_ID_FILENAME = 5
  26. };
  27. static bool OpenDatabase()
  28. {
  29. AutoLock lock(dbcs);
  30. if (!db)
  31. db = NDE_CreateDatabase();
  32. return true;
  33. }
  34. void CloseDatabase()
  35. {
  36. AutoLock lock( dbcs );
  37. if ( db )
  38. {
  39. if ( table )
  40. NDE_Database_CloseTable( db, table );
  41. NDE_DestroyDatabase( db );
  42. }
  43. db = 0;
  44. }
  45. static void CreateFields( nde_table_t table )
  46. {
  47. // create defaults
  48. NDE_Table_NewColumnW( table, DB_ID_SOURCE, L"source", FIELD_STRING );
  49. NDE_Table_NewColumnW( table, DB_ID_URL, L"url", FIELD_STRING );
  50. NDE_Table_NewColumnW( table, DB_ID_TITLE, L"title", FIELD_STRING );
  51. NDE_Table_NewColumnW( table, DB_ID_DOWNLOADDATE, L"downloaddate", FIELD_DATETIME );
  52. NDE_Table_NewColumnW( table, DB_ID_LENGTH, L"length", FIELD_INTEGER );
  53. NDE_Table_NewColumnW( table, DB_ID_FILENAME, L"filename", FIELD_FILENAME );
  54. NDE_Table_PostColumns( table );
  55. NDE_Table_AddIndexByIDW( table, DB_ID_URL, L"url" );
  56. }
  57. static bool OpenTable()
  58. {
  59. AutoLock lock( dbcs );
  60. if ( !OpenDatabase() )
  61. return false;
  62. if ( !table )
  63. {
  64. const wchar_t *inidir = WASABI_API_APP->path_getUserSettingsPath();
  65. wchar_t tablePath[ MAX_PATH ] = { 0 }, indexPath[ MAX_PATH ] = { 0 };
  66. PathCombineW( tablePath, inidir, L"plugins" );
  67. PathAppendW( tablePath, L"downloads.dat" );
  68. PathCombineW( indexPath, inidir, L"plugins" );
  69. PathAppendW( indexPath, L"downloads.idx" );
  70. table = NDE_Database_OpenTable( db, tablePath, indexPath, NDE_OPEN_ALWAYS, NDE_CACHE );
  71. if ( table )
  72. CreateFields( table );
  73. }
  74. return !!table;
  75. }
  76. static void db_add( nde_scanner_t s, unsigned char id, wchar_t *data )
  77. {
  78. if ( data )
  79. {
  80. nde_field_t f = NDE_Scanner_NewFieldByID( s, id );
  81. NDE_StringField_SetString( f, data );
  82. }
  83. }
  84. static void db_add_int( nde_scanner_t s, unsigned char id, int data )
  85. {
  86. if ( data )
  87. {
  88. nde_field_t f = NDE_Scanner_NewFieldByID( s, id );
  89. NDE_IntegerField_SetValue( f, data );
  90. }
  91. }
  92. static void db_add_time( nde_scanner_t s, unsigned char id, time_t data )
  93. {
  94. if ( data )
  95. {
  96. nde_field_t f = NDE_Scanner_NewFieldByID( s, id );
  97. NDE_IntegerField_SetValue( f, static_cast<int>( data ) );
  98. }
  99. }
  100. bool AddDownloadData( const DownloadedFile &data )
  101. {
  102. AutoLock lock( dbcs );
  103. if ( !OpenTable() )
  104. return false;
  105. nde_scanner_t s = NDE_Table_CreateScanner( table );
  106. if ( s )
  107. {
  108. NDE_Scanner_New( s );
  109. db_add( s, DB_ID_SOURCE, data.source );
  110. db_add( s, DB_ID_URL, data.url );
  111. db_add( s, DB_ID_TITLE, data.title );
  112. db_add_time( s, DB_ID_DOWNLOADDATE, data.downloadDate );
  113. db_add_int( s, DB_ID_LENGTH, (int)data.totalSize );
  114. db_add( s, DB_ID_FILENAME, data.path );
  115. NDE_Scanner_Post( s );
  116. NDE_Table_DestroyScanner( table, s );
  117. NDE_Table_Sync( table );
  118. return true;
  119. }
  120. return false;
  121. }
  122. void CompactDatabase()
  123. {
  124. AutoLock lock( dbcs );
  125. if ( OpenTable() )
  126. NDE_Table_Compact( table );
  127. }