PageRenderTime 116ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Miranda IM - CK Release/Miranda/Plugins/UinfoEx/src/svc_email.cpp

http://miranda-dev.googlecode.com/
C++ | 410 lines | 268 code | 35 blank | 107 comment | 48 complexity | f85d2082c7700e9e2a3e465945ad5f5c MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. UserinfoEx plugin for Miranda IM
  3. Copyright:
  4. ? 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. ===============================================================================
  17. File name : $HeadURL: http://userinfoex.googlecode.com/svn/trunk/svc_email.cpp $
  18. Revision : $Revision: 211 $
  19. Last change on : $Date: 2011-04-04 21:10:59 +0400 (??, 04 ??? 2011) $
  20. Last change by : $Author: kreol13@gmail.com $
  21. ===============================================================================
  22. */
  23. #include "commonheaders.h"
  24. #include "m_email.h"
  25. static HANDLE ghMenuItem = NULL;
  26. static HANDLE ghExtraIconDef = INVALID_HANDLE_VALUE;
  27. static HANDLE ghExtraIconSvc = INVALID_HANDLE_VALUE;
  28. static HANDLE hChangedHook = NULL;
  29. static HANDLE hApplyIconHook = NULL;
  30. static HANDLE hRebuildIconsHook = NULL;
  31. HANDLE hPrebuildMenuHook = NULL;
  32. /**
  33. * This function reads the email address of the contact.
  34. *
  35. * @param hContact - handle to contact to read email from
  36. *
  37. * @retval email address
  38. * @retval NULL, if contact does not provide any email address
  39. **/
  40. static LPSTR Get(HANDLE hContact)
  41. {
  42. // ignore owner
  43. if (hContact != NULL)
  44. {
  45. LPCSTR pszProto = DB::Contact::Proto(hContact);
  46. if (pszProto != NULL)
  47. {
  48. LPCSTR e[2][4] = {
  49. { SET_CONTACT_EMAIL, SET_CONTACT_EMAIL0, SET_CONTACT_EMAIL1, "Mye-mail0"},
  50. { SET_CONTACT_COMPANY_EMAIL, SET_CONTACT_COMPANY_EMAIL0, SET_CONTACT_COMPANY_EMAIL1, "MyCompanye-mail0"}
  51. };
  52. INT i, j;
  53. LPSTR pszEMail;
  54. for (i = 0; i < 2; i++)
  55. {
  56. for (j = 0; j < 4; j++)
  57. {
  58. pszEMail = DB::Setting::GetAStringEx(hContact, USERINFO, pszProto, e[i][j]);
  59. if (pszEMail)
  60. {
  61. if (strchr(pszEMail, '@'))
  62. {
  63. return pszEMail;
  64. }
  65. mir_free(pszEMail);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. return NULL;
  72. }
  73. /**
  74. * Service function that sends emails
  75. *
  76. * @param wParam - handle to contact to send an email to
  77. * @param lParam - not used
  78. *
  79. * @retval 0 if email was sent
  80. * @retval 1 if no email can be sent
  81. **/
  82. static INT_PTR MenuCommand(WPARAM wParam,LPARAM lParam)
  83. {
  84. INT result;
  85. LPSTR val = NULL;
  86. __try
  87. {
  88. val = Get((HANDLE) wParam);
  89. if (val)
  90. {
  91. LPSTR szUrl;
  92. INT_PTR len;
  93. len = mir_strlen(val) + strlen("mailto:");
  94. szUrl = (LPSTR)_alloca(len + 1);
  95. mir_snprintf(szUrl, len + 1, "mailto:%s", val);
  96. mir_free(val);
  97. result = CallService(MS_UTILS_OPENURL, 1, (LPARAM)szUrl);
  98. }
  99. else
  100. {
  101. result = 1;
  102. MsgBox((HWND)lParam, MB_OK, LPGENT("Send e-mail"), NULL, LPGENT("Memory allocation error!"));
  103. }
  104. }
  105. __except(GetExceptionCode()==EXCEPTION_ACCESS_VIOLATION ?
  106. EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  107. {
  108. if (val)
  109. {
  110. mir_free(val);
  111. }
  112. result = 1;
  113. MsgErr((HWND)lParam, LPGENT("Memory allocation error!"));
  114. }
  115. return result;
  116. }
  117. /***********************************************************************************************************
  118. * Event Handler functions
  119. ***********************************************************************************************************/
  120. static INT OnCListRebuildIcons(WPARAM wParam, LPARAM lParam)
  121. {
  122. HICON hIcon = IcoLib_GetIcon(ICO_BTN_EMAIL);
  123. ghExtraIconDef = (HANDLE)CallService(MS_CLIST_EXTRA_ADD_ICON, (WPARAM)hIcon, 0);
  124. CallService(MS_SKIN2_RELEASEICON,(WPARAM)hIcon,0);
  125. return 0;
  126. }
  127. /**
  128. * Notification handler for clist extra icons to be applied for a contact.
  129. *
  130. * @param wParam - handle to the contact whose extra icon is to apply
  131. * @param lParam - not used
  132. **/
  133. static INT OnCListApplyIcons(WPARAM wParam, LPARAM lParam)
  134. {
  135. LPSTR val = Get((HANDLE)wParam);
  136. if (!myGlobals.ExtraIconsServiceExist)
  137. {
  138. IconExtraColumn iec;
  139. iec.cbSize = sizeof(IconExtraColumn);
  140. iec.ColumnType = EXTRA_ICON_EMAIL;
  141. if (val)
  142. {
  143. iec.hImage = ghExtraIconDef;
  144. mir_free(val);
  145. }
  146. else
  147. {
  148. iec.hImage = INVALID_HANDLE_VALUE;
  149. mir_free(val);
  150. }
  151. CallService(MS_CLIST_EXTRA_SET_ICON, wParam, (LPARAM)&iec);
  152. }
  153. else
  154. {
  155. EXTRAICON ico;
  156. ico.cbSize=sizeof(ico);
  157. ico.hContact=(HANDLE)wParam;
  158. ico.hExtraIcon=ghExtraIconSvc;
  159. ico.icoName=val?ICO_BTN_EMAIL:(char *)0;
  160. mir_free(val);
  161. CallService(MS_EXTRAICON_SET_ICON, (WPARAM)&ico, 0);
  162. }
  163. return 0;
  164. }
  165. /**
  166. * Notification handler for changed contact settings
  167. *
  168. * @param wParam - (HANDLE)hContact
  169. * @param lParam - (DBCONTACTWRITESETTING*)pdbcws
  170. **/
  171. static INT OnContactSettingChanged(HANDLE hContact, DBCONTACTWRITESETTING* pdbcws)
  172. {
  173. if (hContact && pdbcws && pdbcws->szSetting &&
  174. ((pdbcws->value.type & DBVTF_VARIABLELENGTH) || (pdbcws->value.type == DBVT_DELETED)) &&
  175. (!mir_strncmp(pdbcws->szSetting, SET_CONTACT_EMAIL, 6) ||
  176. !mir_strncmp(pdbcws->szSetting, SET_CONTACT_COMPANY_EMAIL, 13) ||
  177. !mir_strncmp(pdbcws->szSetting, "mye-mail0", 9)))
  178. {
  179. OnCListApplyIcons((WPARAM)hContact, 0);
  180. }
  181. return 0;
  182. }
  183. /**
  184. * This function decides whether to show menuitem for sending emails or not.
  185. *
  186. * @param wParam - handle to contact to send an email to
  187. * @param lParam - not used
  188. *
  189. * @return always 0
  190. **/
  191. static INT OnPreBuildMenu(WPARAM wParam, LPARAM lParam)
  192. {
  193. CLISTMENUITEM mi;
  194. LPSTR val;
  195. ZeroMemory(&mi, sizeof(mi));
  196. mi.cbSize = sizeof(mi);
  197. mi.flags = CMIM_FLAGS;
  198. val = Get((HANDLE)wParam);
  199. if (val)
  200. {
  201. mir_free(val);
  202. }
  203. else
  204. {
  205. mi.flags = CMIM_FLAGS | CMIF_HIDDEN;
  206. }
  207. CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)ghMenuItem, (LPARAM)&mi);
  208. return 0;
  209. }
  210. /***********************************************************************************************************
  211. * public Module Interface functions
  212. ***********************************************************************************************************/
  213. /**
  214. * This function enables or disables menuitems.
  215. **/
  216. VOID SvcEMailRebuildMenu()
  217. {
  218. if (DB::Setting::GetByte(SET_EXTENDED_EMAILSERVICE, TRUE))
  219. {
  220. if (!hPrebuildMenuHook)
  221. {
  222. hPrebuildMenuHook = HookEvent(ME_CLIST_PREBUILDCONTACTMENU, OnPreBuildMenu);
  223. }
  224. if (!ghMenuItem)
  225. {
  226. CLISTMENUITEM mi;
  227. // insert contact menuitem
  228. ZeroMemory(&mi, sizeof(mi));
  229. mi.cbSize = sizeof(mi);
  230. mi.position = -2000010000;
  231. mi.hIcon = IcoLib_GetIcon(ICO_BTN_EMAIL);
  232. mi.pszName = "&E-mail";
  233. mi.pszService = MS_EMAIL_SENDEMAIL;
  234. ghMenuItem = (HANDLE)CallService(MS_CLIST_ADDCONTACTMENUITEM, 0, (LPARAM)&mi);
  235. }
  236. }
  237. else
  238. {
  239. if (hPrebuildMenuHook)
  240. UnhookEvent(hPrebuildMenuHook);
  241. if (ghMenuItem)
  242. {
  243. CallService(MS_CLIST_REMOVECONTACTMENUITEM, (WPARAM)ghMenuItem, NULL);
  244. ghMenuItem = NULL;
  245. }
  246. }
  247. }
  248. /**
  249. * Force all icons to be reloaded.
  250. *
  251. * @param wParam - handle to the contact whose extra icon is to apply
  252. * @param lParam - not used
  253. **/
  254. VOID SvcEMailApplyCListIcons()
  255. {
  256. HANDLE hContact;
  257. //walk through all the contacts stored in the DB
  258. for (hContact = DB::Contact::FindFirst(); hContact != NULL; hContact = DB::Contact::FindNext(hContact))
  259. {
  260. OnCListApplyIcons((WPARAM)hContact, 0);
  261. }
  262. }
  263. /**
  264. * Enable or disable the replacement of clist extra icons.
  265. *
  266. * @param bEnable - determines whether icons are enabled or not
  267. * @param bUpdateDB - if true the database setting is updated, too.
  268. **/
  269. VOID SvcEMailEnableExtraIcons(BOOLEAN bEnable, BOOLEAN bUpdateDB)
  270. {
  271. if (myGlobals.HaveCListExtraIcons)
  272. {
  273. if (bUpdateDB)
  274. {
  275. DB::Setting::WriteByte(SET_CLIST_EXTRAICON_EMAIL, bEnable);
  276. }
  277. if (bEnable) // E-mail checkt
  278. {
  279. // hook events
  280. if (hChangedHook == NULL)
  281. {
  282. hChangedHook = HookEvent(ME_DB_CONTACT_SETTINGCHANGED, (MIRANDAHOOK)OnContactSettingChanged);
  283. }
  284. if (hApplyIconHook == NULL)
  285. {
  286. hApplyIconHook = HookEvent(ME_CLIST_EXTRA_IMAGE_APPLY, OnCListApplyIcons);
  287. }
  288. if (myGlobals.ExtraIconsServiceExist)
  289. {
  290. if (ghExtraIconSvc == INVALID_HANDLE_VALUE)
  291. {
  292. EXTRAICON_INFO ico;
  293. ZeroMemory(&ico, sizeof(ico));
  294. ico.cbSize = sizeof(ico);
  295. ico.type = EXTRAICON_TYPE_ICOLIB;
  296. ico.name = "email"; //must be the same as the group name in extraicon
  297. ico.description= "E-mail (uinfoex)";
  298. ico.descIcon = ICO_BTN_EMAIL;
  299. ghExtraIconSvc = (HANDLE)CallService(MS_EXTRAICON_REGISTER, (WPARAM)&ico, 0);
  300. ZeroMemory(&ico,sizeof(ico));
  301. }
  302. }
  303. else if (hRebuildIconsHook == NULL)
  304. {
  305. hRebuildIconsHook = HookEvent(ME_CLIST_EXTRA_LIST_REBUILD, OnCListRebuildIcons);
  306. OnCListRebuildIcons(0, 0);
  307. }
  308. }
  309. else // E-mail uncheckt
  310. {
  311. if (hChangedHook)
  312. {
  313. UnhookEvent(hChangedHook);
  314. hChangedHook = NULL;
  315. }
  316. if (hApplyIconHook)
  317. {
  318. UnhookEvent(hApplyIconHook);
  319. hApplyIconHook = NULL;
  320. }
  321. if (hRebuildIconsHook)
  322. {
  323. UnhookEvent(hRebuildIconsHook);
  324. hRebuildIconsHook = NULL;
  325. }
  326. }
  327. SvcEMailApplyCListIcons();
  328. }
  329. }
  330. /**
  331. * This function initially loads the module uppon startup.
  332. **/
  333. VOID SvcEMailOnModulesLoaded()
  334. {
  335. SvcEMailEnableExtraIcons(
  336. myGlobals.ExtraIconsServiceExist ||
  337. DB::Setting::GetByte(SET_CLIST_EXTRAICON_EMAIL,
  338. DEFVAL_CLIST_EXTRAICON_EMAIL), FALSE);
  339. }
  340. /**
  341. * This function initially loads the module uppon startup.
  342. **/
  343. VOID SvcEMailLoadModule()
  344. {
  345. if (DB::Setting::GetByte(SET_EXTENDED_EMAILSERVICE, TRUE))
  346. {
  347. // prevent default email module from loading
  348. PINT_PTR disableDefaultModule = (PINT_PTR)CallService(MS_PLUGINS_GETDISABLEDEFAULTARRAY, 0, 0);
  349. disableDefaultModule[DEFMOD_SREMAIL] = TRUE;
  350. // create own email send command
  351. if(!myDestroyServiceFunction(MS_EMAIL_SENDEMAIL))
  352. myCreateServiceFunction(MS_EMAIL_SENDEMAIL, MenuCommand);
  353. }
  354. }
  355. /**
  356. * This function unloads the Email module.
  357. *
  358. * @param none
  359. *
  360. * @return nothing
  361. **/
  362. VOID SvcEMailUnloadModule()
  363. {
  364. // unhook event handlers
  365. UnhookEvent(hChangedHook); hChangedHook = NULL;
  366. UnhookEvent(hApplyIconHook); hApplyIconHook = NULL;
  367. UnhookEvent(hRebuildIconsHook); hRebuildIconsHook = NULL;
  368. }