aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_wire/db.cpp
blob: d46b2a7dfbfb869d91e048a86a86469f325f7d8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <shlwapi.h>

#include "api__ml_wire.h"
#include "Downloaded.h"
#include "../nde/nde_c.h"
#include "../nu/AutoLock.h"

/* DB Schema
ChannelTitle
ItemUrl
ItemTitle
PublishDate
Length
Filename
*/

static Nullsoft::Utility::LockGuard dbcs;
static nde_table_t table = 0;
static nde_database_t db = 0;
using namespace Nullsoft::Utility;

enum
{
	DB_ID_CHANNELTITLE = 0,
	DB_ID_ITEMURL      = 1,
	DB_ID_ITEMTITLE    = 2,
	DB_ID_PUBLISHDATE  = 3,
	DB_ID_LENGTH       = 4,
	DB_ID_FILENAME     = 5
};

static bool OpenDatabase()
{
	AutoLock lock(dbcs);
	if (!db)
		db = NDE_CreateDatabase();

	return true;
}

void CloseDatabase()
{
	AutoLock lock( dbcs );
	if ( db )
	{
		if ( table )
			NDE_Database_CloseTable( db, table );

		NDE_DestroyDatabase( db );
	}

	db = 0;
}

static void CreateFields(nde_table_t table)
{
	// create defaults
	NDE_Table_NewColumnW(table, DB_ID_CHANNELTITLE, L"channeltitle", FIELD_STRING);
	NDE_Table_NewColumnW(table, DB_ID_ITEMURL,      L"itemurl",      FIELD_STRING);
	NDE_Table_NewColumnW(table, DB_ID_ITEMTITLE,    L"itemtitle",    FIELD_STRING);
	NDE_Table_NewColumnW(table, DB_ID_PUBLISHDATE,  L"publishdate",  FIELD_DATETIME);
	NDE_Table_NewColumnW(table, DB_ID_LENGTH,       L"length",       FIELD_INTEGER);
	NDE_Table_NewColumnW(table, DB_ID_FILENAME,     L"filename",     FIELD_FILENAME);
	NDE_Table_PostColumns(table);
	NDE_Table_AddIndexByIDW(table, DB_ID_ITEMURL, L"itemurl");
}

static bool OpenTable()
{
	AutoLock lock( dbcs );
	if ( !OpenDatabase() )
		return false;

	if ( !table )
	{
		const wchar_t *inidir = WASABI_API_APP->path_getUserSettingsPath();
		wchar_t tablePath[ MAX_PATH ] = { 0 }, indexPath[ MAX_PATH ] = { 0 };

		PathCombineW( tablePath, inidir, L"plugins" );
		PathAppendW( tablePath, L"podcasts.dat" );
		PathCombineW( indexPath, inidir, L"plugins" );
		PathAppendW( indexPath, L"podcasts.idx" );

		table = NDE_Database_OpenTable( db, tablePath, indexPath, NDE_OPEN_ALWAYS, NDE_CACHE );
		if ( table )
			CreateFields( table );
	}

	return !!table;
}

static void db_add( nde_scanner_t s, unsigned char id, wchar_t *data )
{
	if ( data )
	{
		nde_field_t f = NDE_Scanner_NewFieldByID( s, id );
		NDE_StringField_SetString( f, data );
	}
}

static void db_add_int( nde_scanner_t s, unsigned char id, int data )
{
	if ( data )
	{
		nde_field_t f = NDE_Scanner_NewFieldByID( s, id );
		NDE_IntegerField_SetValue( f, data );
	}
}

static void db_add_time( nde_scanner_t s, unsigned char id, time_t data )
{
	if ( data )
	{
		nde_field_t f = NDE_Scanner_NewFieldByID( s, id );
		NDE_IntegerField_SetValue( f, static_cast<int>( data ) );
	}
}

bool AddPodcastData( const DownloadedFile &data )
{
	AutoLock lock( dbcs );
	if ( !OpenTable() )
		return false;

	nde_scanner_t s = NDE_Table_CreateScanner( table );
	if ( s )
	{
		NDE_Scanner_New( s );
		db_add( s, DB_ID_CHANNELTITLE, data.channel );
		db_add( s, DB_ID_ITEMURL, data.url );
		db_add( s, DB_ID_ITEMTITLE, data.item );
		db_add_time( s, DB_ID_PUBLISHDATE, data.publishDate );
		db_add_int( s, DB_ID_LENGTH, (int)data.totalSize );
		db_add( s, DB_ID_FILENAME, data.path );
		NDE_Scanner_Post( s );
		NDE_Table_DestroyScanner( table, s );
		NDE_Table_Sync( table );

		return true;
	}

	return false;
}

bool IsPodcastDownloaded( const wchar_t *url )
{
	AutoLock lock( dbcs );
	if ( !OpenTable() )
		return false;

	nde_scanner_t s = NDE_Table_CreateScanner( table );
	if ( s )
	{
		if ( NDE_Scanner_LocateString( s, DB_ID_ITEMURL, FIRST_RECORD, url ) )
		{
			NDE_Table_DestroyScanner( table, s );
			return true;
		}

		NDE_Table_DestroyScanner( table, s );
	}

	return false;
}

void CompactDatabase()
{
	AutoLock lock( dbcs );
	if ( OpenTable() )
		NDE_Table_Compact( table );
}