summaryrefslogtreecommitdiff
path: root/plugins/Folders/src/services.cpp
blob: 4ec1733b79ddbb26820dc56329ca7ec19792f9c1 (plain)
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
/*
Custom profile folders plugin for Miranda IM

Copyright © 2005 Cristian Libotean

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "services.h"

#define DEFAULT_SECTION "Unknown"

TCHAR szCurrentProfilePath[MAX_FOLDERS_PATH];
TCHAR szCurrentProfile[MAX_FOLDERS_PATH];
TCHAR szMirandaPath[MAX_FOLDERS_PATH];
TCHAR szUserDataPath[MAX_FOLDERS_PATH];

HANDLE hsFoldersGetPath;
HANDLE hsFoldersGetSize;
HANDLE hsFoldersGetPathAlloc;
HANDLE hsFoldersRegisterPath;

INT_PTR ExpandPath(TCHAR *szResult, TCHAR *format, int size)
{
	szResult[0] = '\0';

	TCHAR *input = NULL;
	if ( ServiceExists(MS_VARS_FORMATSTRING))
		input = variables_parse(format, NULL, NULL);

	if (input == NULL)
		input = mir_tstrdup(format);

	TCHAR *core_result = Utils_ReplaceVarsT(input);
	_tcsncpy(szResult, core_result, size);
	
	mir_free(core_result);

	StrReplace(szResult, PROFILE_PATHT,     szCurrentProfilePath);
	StrReplace(szResult, CURRENT_PROFILET,  szCurrentProfile);
	StrReplace(szResult, MIRANDA_PATHT,     szMirandaPath);
	StrReplace(szResult, MIRANDA_USERDATAT, szUserDataPath);

	StrTrim(szResult, _T("\t \\"));

	mir_free(input);

	return _tcslen(szResult);
}

INT_PTR RegisterPathService(WPARAM wParam, LPARAM lParam)
{
	FOLDERSDATA *tmp = (FOLDERSDATA *) lParam;
	if (tmp == NULL || tmp->cbSize != sizeof(FOLDERSDATA))
		return NULL;

	return lstRegisteredFolders.Add(tmp); //returns 1..n or 0 on error
}

INT_PTR GetPathSizeService(WPARAM wParam, LPARAM lParam)
{
	TCHAR tmp[MAX_FOLDER_SIZE];
	int res = lstRegisteredFolders.Expand(wParam, tmp, SIZEOF(tmp));
	size_t len = _tcslen(tmp);

	if (lParam != NULL)
		*((size_t *) lParam) = len;

	return len;
}

INT_PTR GetPathService(WPARAM wParam, LPARAM lParam)
{
	FOLDERSGETDATA* data = (FOLDERSGETDATA *) lParam;
	if (data->cbSize != sizeof	(FOLDERSGETDATA))
		return 1;

	if (data->flags & FF_UNICODE)
		return lstRegisteredFolders.Expand(wParam, data->szPathT, data->nMaxPathSize);

	TCHAR buf[MAX_FOLDER_SIZE];
	if ( lstRegisteredFolders.Expand(wParam, buf, MAX_FOLDER_SIZE))
		return 1;

	strncpy(data->szPath, _T2A(buf), data->nMaxPathSize);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

int InitServices()
{
	CallService(MS_DB_GETPROFILEPATHT, SIZEOF(szCurrentProfilePath), (LPARAM)szCurrentProfilePath);
	CallService(MS_DB_GETPROFILENAMET, SIZEOF(szCurrentProfile), (LPARAM)szCurrentProfile);
	TCHAR *pos = _tcsrchr(szCurrentProfile, '.'); if (pos) *pos = 0;

	GetModuleFileName( GetModuleHandle(NULL), szMirandaPath, SIZEOF(szMirandaPath));
	pos = _tcsrchr(szMirandaPath, '\\'); if (pos) *pos = 0;

	TCHAR *szTemp = Utils_ReplaceVarsT( _T("%miranda_userdata%"));
	mir_sntprintf(szUserDataPath, MAX_FOLDERS_PATH, szTemp);
	mir_free(szTemp);

	hsFoldersGetPath = CreateServiceFunction(MS_FOLDERS_GET_PATH, GetPathService);
	hsFoldersGetSize = CreateServiceFunction(MS_FOLDERS_GET_SIZE, GetPathSizeService);
	hsFoldersRegisterPath = CreateServiceFunction(MS_FOLDERS_REGISTER_PATH, RegisterPathService);
	return 0;
}

int DestroyServices()
{
	DestroyServiceFunction(hsFoldersGetPath);
	DestroyServiceFunction(hsFoldersGetSize);
	DestroyServiceFunction(hsFoldersRegisterPath);
	return 0;
}