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
|
//***************************************************************************************
//
// Google Extension plugin for the Miranda IM's Jabber protocol
// Copyright (c) 2011 bems@jabber.org, George Hazan (ghazan@jabber.ru)
//
// 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 "stdafx.h"
#include "avatar.h"
#include "resources.h"
#include "options.h"
static const LPSTR AVA_FILE_NAME_FORMAT = "%s\\%s\\AvatarCache\\Jabber\\" SHORT_PLUGIN_NAME ".pseudoava.png";
static const LPTSTR AVA_RES_TYPE = _T("PNG");
static const LPSTR SRMM_MODULE_NAME = "SRMM";
static const LPSTR SRMM_AVATAR_SETTING_NAME = "Avatar";
static const int SET_AVATAR_INTERVAL = 2000;
void ForceDir(LPSTR dir, int len)
{
if (GetFileAttributesA(dir) != INVALID_FILE_ATTRIBUTES) return;
for (int i = len - 1; i >= 0; i--)
if ('\\' == dir[i]) {
dir[i] = 0;
__try {
ForceDir(dir, i);
}
__finally {
dir[i] = '\\';
}
CreateDirectoryA(dir, NULL);
}
}
void ForceFileDir(LPSTR file)
{
for (int i = lstrlenA(file) - 1; i >= 0; i--)
if ('\\' == file[i]) {
file[i] = 0;
__try {
ForceDir(file, i);
}
__finally {
file[i] = '\\';
}
break;
}
}
LPSTR CreateAvaFile(HANDLE *hFile)
{
char name[MAX_PATH + 2];
char path[MAX_PATH + 2];
char full[MAX_PATH + 2];
if (CallService(MS_DB_GETPROFILENAME, (WPARAM)sizeof(name), (LPARAM)&name))
return NULL;
for (int i = lstrlenA(name); i >= 0; i--)
if ('.' == name[i]) {
name[i] = 0;
break;
}
if (CallService(MS_DB_GETPROFILEPATH, (WPARAM)sizeof(path), (LPARAM)&path))
return NULL;
sprintf(&full[0], AVA_FILE_NAME_FORMAT, path, name);
ForceFileDir(&full[0]);
HANDLE h = 0;
__try {
h = CreateFileA(&full[0], GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE == h) return NULL;
if (hFile) *hFile = h; else CloseHandle(h);
h = 0;
return _strdup(&full[0]);
}
__finally {
CloseHandle(h);
}
}
extern HINSTANCE hInst;
BOOL SaveAvatar(HANDLE hFile)
{
HRSRC hres = FindResource(hInst, MAKEINTRESOURCE(IDI_PSEUDOAVA), AVA_RES_TYPE);
if (!hres) return FALSE;
HGLOBAL hglob = LoadResource(hInst, hres);
if (!hglob) return FALSE;
PVOID p = LockResource(hglob);
if (!p) return FALSE;
DWORD l = SizeofResource(hInst, hres);
if (!l) return FALSE;
DWORD written;
if (!WriteFile(hFile, p, l, &written, NULL)) return FALSE;
return written == l;
}
struct AVACHANGED {
HANDLE hTimer;
HANDLE hContact;
};
VOID CALLBACK CallSetAvatar(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
{
CallService(MS_SYSTEM_THREAD_PUSH, 0, 0);
__try {
AVACHANGED *ach = (AVACHANGED*)lpParameter;
__try {
SetAvatar(ach->hContact);
DeleteTimerQueueTimer(NULL, ach->hTimer, NULL);
}
__finally {
free(ach);
}
}
__finally {
CallService(MS_SYSTEM_THREAD_POP, 0, 0);
}
}
int AvaChanged(WPARAM wParam, LPARAM lParam)
{
if (!lParam && DBGetContactSettingByte((HANDLE)wParam, SHORT_PLUGIN_NAME, PSEUDOCONTACT_FLAG, 0)) {
BOOL enqueued = FALSE;
AVACHANGED *ach = (AVACHANGED*)malloc(sizeof(AVACHANGED));
__try {
ach->hContact = (HANDLE)wParam;
enqueued = CreateTimerQueueTimer(&ach->hTimer, NULL, CallSetAvatar, ach, SET_AVATAR_INTERVAL, 0, WT_EXECUTEONLYONCE);
}
__finally {
if (!enqueued) free(ach);
}
}
return 0;
}
CRITICAL_SECTION g_csSetAvatar;
HANDLE hAvaChanged = 0;
BOOL initialized = FALSE;
BOOL InitAvaUnit(BOOL init)
{
if (init) {
hAvaChanged = HookEvent(ME_AV_AVATARCHANGED, AvaChanged);
InitializeCriticalSection(&g_csSetAvatar);
initialized = TRUE;
return hAvaChanged != 0;
}
else {
if (initialized) {
initialized = FALSE;
DeleteCriticalSection(&g_csSetAvatar);
}
if (hAvaChanged) {
UnhookEvent(hAvaChanged);
hAvaChanged = 0;
}
return TRUE;
}
}
void SetAvatar(HANDLE hContact)
{
EnterCriticalSection(&g_csSetAvatar);
__try {
avatarCacheEntry *ava = (avatarCacheEntry*)CallService(MS_AV_GETAVATARBITMAP, (WPARAM)hContact, 0);
if (ava && GetFileAttributes(&ava->szFilename[0]) != INVALID_FILE_ATTRIBUTES)
return;
HANDLE hFile;
LPSTR avaFile = CreateAvaFile(&hFile);
if (avaFile)
__try {
BOOL saved = SaveAvatar(hFile);
CloseHandle(hFile); hFile = 0;
if (saved){
if (ava) CallService(MS_AV_SETAVATAR, (WPARAM)hContact, (LPARAM)"");
CallService(MS_AV_SETAVATAR, (WPARAM)hContact, (LPARAM)avaFile);
DBWriteContactSettingString(hContact, SRMM_MODULE_NAME, SRMM_AVATAR_SETTING_NAME, avaFile);
}
}
__finally {
free(avaFile);
CloseHandle(hFile);
}
}
__finally {
LeaveCriticalSection(&g_csSetAvatar);
}
}
|