aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/SDK/coverdirectory/CoverDirectory.cpp
blob: f68be8465838a91d888c512e60719b815e07400a (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "api.h"
#include "CoverDirectory.h"
#include <api/service/svcs/svc_imgload.h>
#include <api/service/waservicefactory.h>
#include <shlwapi.h>
#include <strsafe.h>

static void CleanNameForPath(wchar_t *name)
{
	while (name && *name)
	{
		switch(*name)
		{
		case L'?':
		case L'*':
		case  L'|':
			*name = L'_';
			break;
		case '/':
		case L'\\':
		case L':':
			*name =  L'-';
			break;
		case L'\"': 
			*name  = L'\'';
			break;
		case L'<':
			*name  = L'(';
			break;
		case L'>': *name = L')';
			break;
		}
		name++;			
	}	
}

/* This is just hardcoded for now - Search the albumart in a cover library dir */
static bool BuildCoverFilename(const wchar_t *filename, const wchar_t *type, wchar_t path[MAX_PATH], wchar_t mask[MAX_PATH])
{
	// TODO: use tagz for this.
	wchar_t artistname[MAX_PATH]=L"", albumname[MAX_PATH]=L"";
	// benski> we're abusing short-circuit evaluation in a big way here :)
  if (((AGAVE_API_METADATA->GetExtendedFileInfo(filename, L"albumartist", artistname, MAX_PATH) && artistname[0]) 
		 || (AGAVE_API_METADATA->GetExtendedFileInfo(filename, L"artist", artistname, MAX_PATH) && artistname[0]))
		 && AGAVE_API_METADATA->GetExtendedFileInfo(filename, L"album", albumname, MAX_PATH) && albumname[0])
	{
		CleanNameForPath(artistname);
		CleanNameForPath(albumname);
		StringCchPrintf(mask, MAX_PATH, L"%s - %s.*",artistname, albumname);
		PathCombine(path, WASABI_API_APP->path_getUserSettingsPath(), L"Cover Library");
		return true;
	}
	return false;
}

static svc_imageLoader *FindImageLoader(const wchar_t *filespec, waServiceFactory **factory)
{
	FOURCC imgload = svc_imageLoader::getServiceType();
	int n = WASABI_API_SVC->service_getNumServices(imgload);
	for (int i=0; i<n; i++)
	{
		waServiceFactory *sf = WASABI_API_SVC->service_enumService(imgload,i);
		if (sf)
		{	
			svc_imageLoader * l = (svc_imageLoader*)sf->getInterface();
			if (l)
			{
				if (l->isMine(filespec))
				{
					*factory = sf;
					return l;
				}
				sf->releaseInterface(l);
			}
		}
	}
	return NULL;
}

static bool ImageExists(const wchar_t *path, const wchar_t *mask)
{
	wchar_t dirmask[MAX_PATH];
	PathCombineW(dirmask, path, mask);
	WIN32_FIND_DATAW find;
	HANDLE hFind = FindFirstFileW(dirmask, &find);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			wchar_t fn[MAX_PATH];
			PathCombine(fn, path, mask);
			waServiceFactory *factory=0;
			svc_imageLoader *loader = FindImageLoader(fn, &factory);
			if (loader)
			{
				factory->releaseInterface(loader);
				FindClose(hFind);
				return true;
			}
			
		}
		while (FindNextFileW(hFind, &find));
		FindClose(hFind);
	}
	return false;
}

static bool LoadFile(const wchar_t *filename, void **bits, size_t *len)
{
	*len=0;
	FILE * f = _wfopen(filename,L"rb");
	if (!f)
		return false;
	fseek(f,0,2);
	*len = ftell(f);
	if (!*len)
	{
		fclose(f);
		return false;
	}
	fseek(f,0,0);
	void * data = WASABI_API_MEMMGR->sysMalloc(*len);
	fread(data,*len,1,f);
	fclose(f);
	*bits = data;
	return true;
}

static bool LoadImageData(const wchar_t *path, const wchar_t *mask, void **bits, size_t *len, wchar_t **mimeType)
{
	wchar_t dirmask[MAX_PATH];
	PathCombineW(dirmask, path, mask);
	WIN32_FIND_DATAW find;
	HANDLE hFind = FindFirstFileW(dirmask, &find);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			wchar_t fn[MAX_PATH];
			PathCombine(fn, path, find.cFileName);
			waServiceFactory *factory=0;
			svc_imageLoader *loader = FindImageLoader(fn, &factory);
			if (loader)
			{
				factory->releaseInterface(loader);
				if (LoadFile(fn, bits, len))
				{
					const wchar_t *ext = PathFindExtension(fn);
					if (*ext)
					{
						ext++;
						size_t len = wcslen(ext);
						*mimeType = (wchar_t *)WASABI_API_MEMMGR->sysMalloc((len + 1) * sizeof(wchar_t));
						StringCchCopy(*mimeType, len+1, ext);
						CharLower(*mimeType);
					}
					else
					{
						*mimeType=0;
					}

					FindClose(hFind);
					return true;
				}
			}
			
		}
		while (FindNextFileW(hFind, &find));
		FindClose(hFind);
	}
	return false;
}

static bool DeleteImage(const wchar_t *path, const wchar_t *mask)
{
	wchar_t dirmask[MAX_PATH];
	PathCombineW(dirmask, path, mask);
	WIN32_FIND_DATAW find;
	HANDLE hFind = FindFirstFileW(dirmask, &find);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			wchar_t fn[MAX_PATH];
			PathCombine(fn, path, mask);
			waServiceFactory *factory=0;
			svc_imageLoader *loader = FindImageLoader(fn, &factory);
			if (loader)
			{
				DeleteFile(fn);
				factory->releaseInterface(loader);
			}			
		}
		while (FindNextFileW(hFind, &find));
		FindClose(hFind);
	}
	return false;
}

bool CoverDirectory::IsMine(const wchar_t *filename)
{
	//wchar_t path[MAX_PATH], mask[MAX_PATH];
	//if (BuildCoverFilename(filename, path, mask) && ImageExists(path, mask))
		return true;
	//else
//		return false;
}

int CoverDirectory::ProviderType()
{
	return ALBUMARTPROVIDER_TYPE_DATABASE;
}

// implementation note: use WASABI_API_MEMMGR to alloc bits and mimetype, so that the recipient can free through that
int CoverDirectory::GetAlbumArtData(const wchar_t *filename, const wchar_t *type, void **bits, size_t *len, wchar_t **mimeType)
{
	wchar_t path[MAX_PATH], mask[MAX_PATH];
	if (BuildCoverFilename(filename, type, path, mask))
	{
		if (LoadImageData(path, mask, bits, len, mimeType))
		{
			return ALBUMARTPROVIDER_SUCCESS;
		}
	}
	return ALBUMARTPROVIDER_FAILURE;
}

int CoverDirectory::SetAlbumArtData(const wchar_t *filename, const wchar_t *type, void *bits, size_t len, const wchar_t *mimeType)
{
  // TODO:
	return ALBUMARTPROVIDER_FAILURE;
}

int CoverDirectory::DeleteAlbumArt(const wchar_t *filename, const wchar_t *type)
{
	wchar_t path[MAX_PATH], mask[MAX_PATH];
	if (BuildCoverFilename(filename, type, path, mask))
	{
		DeleteImage(path, mask);
		return ALBUMARTPROVIDER_SUCCESS;	
	}
	return ALBUMARTPROVIDER_FAILURE;
}

#define CBCLASS CoverDirectory
START_DISPATCH;
CB(SVC_ALBUMARTPROVIDER_PROVIDERTYPE, ProviderType);
CB(SVC_ALBUMARTPROVIDER_GETALBUMARTDATA, GetAlbumArtData);
CB(SVC_ALBUMARTPROVIDER_ISMINE, IsMine);
CB(SVC_ALBUMARTPROVIDER_SETALBUMARTDATA, SetAlbumArtData);
CB(SVC_ALBUMARTPROVIDER_DELETEALBUMART, DeleteAlbumArt);
END_DISPATCH;
#undef CBCLASS

static CoverDirectory albumArtProvider;

// {725084AC-DBAD-4f7b-98FA-478AE20B9517}
static const GUID coverDirectoryGUID = 
{ 0x725084ac, 0xdbad, 0x4f7b, { 0x98, 0xfa, 0x47, 0x8a, 0xe2, 0xb, 0x95, 0x17 } };


FOURCC AlbumArtFactory::GetServiceType()
{
	return svc_albumArtProvider::SERVICETYPE;
}

const char *AlbumArtFactory::GetServiceName()
{
	return "Cover Directory";
}

GUID AlbumArtFactory::GetGUID()
{
	return coverDirectoryGUID;
}

void *AlbumArtFactory::GetInterface(int global_lock)
{
	return &albumArtProvider;
}

int AlbumArtFactory::SupportNonLockingInterface()
{
	return 1;
}

int AlbumArtFactory::ReleaseInterface(void *ifc)
{
	//WASABI_API_SVC->service_unlock(ifc);
	return 1;
}

const char *AlbumArtFactory::GetTestString()
{
	return 0;
}

int AlbumArtFactory::ServiceNotify(int msg, int param1, int param2)
{
	return 1;
}

#define CBCLASS AlbumArtFactory
START_DISPATCH;
CB(WASERVICEFACTORY_GETSERVICETYPE, GetServiceType)
CB(WASERVICEFACTORY_GETSERVICENAME, GetServiceName)
CB(WASERVICEFACTORY_GETGUID, GetGUID)
CB(WASERVICEFACTORY_GETINTERFACE, GetInterface)
CB(WASERVICEFACTORY_SUPPORTNONLOCKINGGETINTERFACE, SupportNonLockingInterface)
CB(WASERVICEFACTORY_RELEASEINTERFACE, ReleaseInterface)
CB(WASERVICEFACTORY_GETTESTSTRING, GetTestString)
CB(WASERVICEFACTORY_SERVICENOTIFY, ServiceNotify)
END_DISPATCH;
#undef CBCLASS