summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/history_menus.cpp
blob: 4e6f67d1c44fd41f28e6d332ec7159ca8fffd3e8 (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
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
/*
Copyright (c) 2005 Victor Pavlychko (nullbyte@sotline.net.ua)
Copyright (C) 2012-23 Miranda NG team (https://miranda-ng.org)

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

enum
{
	MENU_COPY, MENU_COPYTEXT, MENU_COPYURL, MENU_OPENFOLDER, MENU_QUOTE,
	MENU_SAVEAS, MENU_DOWNLOAD,
	MENU_EDIT, MENU_DELETE,
	MENU_SELECTALL, MENU_BOOKMARK,
};

static int hMenuObject;
static HANDLE hEventPreBuildMenu;
static HGENMENU hmiHistory, hmiOpenFolder, hmiCopyUrl, hmiSaveAs, hmiDownload, hmiQuote;
static HGENMENU hmiCopy, hmiCopyText, hmiEdit, hmiBookmark, hmiDelete;

HMENU NSMenu_Build(NewstoryListData *data, ItemData *item)
{
	bool bNotProtected = true;
	if (item != nullptr)
		if (auto *szProto = Proto_GetBaseAccountName(item->hContact))
			bNotProtected = db_get_b(item->hContact, szProto, "Protected") == 0;

	Menu_ShowItem(hmiCopy, bNotProtected);
	Menu_ShowItem(hmiCopyText, bNotProtected);

	Menu_ShowItem(hmiQuote, bNotProtected && data->pMsgDlg != nullptr);
	Menu_ShowItem(hmiSaveAs, false);
	Menu_ShowItem(hmiCopyUrl, false);
	Menu_ShowItem(hmiDownload, false);
	Menu_ShowItem(hmiOpenFolder, false);

	bool bShowEventActions, bEditable;
	if (item != nullptr) {
		if (item->m_bOfflineFile) {
			Menu_ModifyItem(hmiCopyUrl, (item->m_bOfflineDownloaded) ? TranslateT("Copy file path") : TranslateT("Copy URL"));
			Menu_ShowItem(hmiCopyUrl, true);
			Menu_ShowItem(hmiSaveAs, bNotProtected);
			Menu_ShowItem(hmiDownload, !item->m_bOfflineDownloaded && bNotProtected);
			Menu_ShowItem(hmiOpenFolder, item->m_bOfflineDownloaded);
		}

		bEditable = (item->dbe.flags & DBEF_SENT) != 0;
		bShowEventActions = item->hEvent != 0;

		DB::EventInfo dbei(item->hEvent);
		NotifyEventHooks(hEventPreBuildMenu, item->hContact, (LPARAM)&dbei);
	}
	else {
		bShowEventActions = bEditable = false;

		DB::EventInfo dbei;
		NotifyEventHooks(hEventPreBuildMenu, 0, (LPARAM)&dbei);
	}

	Menu_ShowItem(hmiEdit, bShowEventActions && bEditable);
	Menu_ShowItem(hmiDelete, bShowEventActions && bEditable);
	Menu_ShowItem(hmiBookmark, bShowEventActions);

	HMENU hMenu = CreatePopupMenu();
	Menu_Build(hMenu, hMenuObject);
	return hMenu;
}

bool NSMenu_Process(int iCommand, NewstoryListData *data)
{
	if (Menu_ProcessCommandById(iCommand, LPARAM(data)))
		return true;

	if (auto *pDlg = data->pMsgDlg) {
		PostMessage(pDlg->GetHwnd(), WM_MOUSEACTIVATE, 0, 0);
		if (Chat_DoEventHook(pDlg->getChat(), GC_USER_LOGMENU, nullptr, nullptr, iCommand))
			return true;
	}

	return false;
}

static INT_PTR NSMenuHelper(WPARAM wParam, LPARAM lParam)
{
	auto *pData = (NewstoryListData *)lParam;

	switch (wParam) {
	case MENU_COPY:
		pData->Copy(false);
		break;

	case MENU_COPYTEXT:
		pData->Copy(true);
		break;

	case MENU_COPYURL:
		pData->CopyUrl();
		break;

	case MENU_OPENFOLDER:
		pData->OpenFolder();
		break;

	case MENU_QUOTE:
		pData->Quote();
		break;

	case MENU_EDIT:
		pData->BeginEditItem();
		break;

	case MENU_DELETE:
		pData->DeleteItems();
		break;

	case MENU_SELECTALL:
		SendMessage(pData->m_hwnd, NSM_SELECTITEMS, 0, pData->totalCount - 1);
		break;

	case MENU_SAVEAS:
		pData->Download(OFD_SAVEAS | OFD_RUN);
		break;

	case MENU_DOWNLOAD:
		pData->Download(OFD_DOWNLOAD);
		break;

	case MENU_BOOKMARK:
		pData->ToggleBookmark();
		PostMessage(GetParent(pData->m_hwnd), UM_BOOKMARKS, 0, 0);
		break;
	}

	return 0;
}

static INT_PTR NSMenuAddService(WPARAM wParam, LPARAM lParam)
{
	auto *pmi = (TMO_MenuItem *)wParam;
	if (pmi == nullptr)
		return 0;

	auto *mmep = (NSMenuExecParam *)mir_calloc(sizeof(NSMenuExecParam));
	if (mmep == nullptr)
		return 0;

	// we need just one parametr.
	mmep->szServiceName = mir_strdup(pmi->pszService);
	mmep->iParam = lParam;

	HGENMENU hNewItem = Menu_AddItem(hMenuObject, pmi, mmep);

	char buf[1024];
	mir_snprintf(buf, "%s/%s", pmi->pszService, pmi->name.a);
	Menu_ConfigureItem(hNewItem, MCI_OPT_UNIQUENAME, buf);
	return (INT_PTR)hNewItem;
}

static INT_PTR NSMenuExecService(WPARAM wParam, LPARAM lParam)
{
	if (auto *pParam = (NSMenuExecParam *)wParam)
		CallService(pParam->szServiceName, pParam->iParam, lParam);

	return 0;
}

static INT_PTR NSMenuFreeOwnerData(WPARAM, LPARAM lParam)
{
	if (auto *param = (NSMenuExecParam *)lParam) {
		mir_free(param->szServiceName);
		mir_free(param);
	}
	return 0;
}

static int OnPrebuildContactMenu(WPARAM hContact, LPARAM)
{
	Menu_ShowItem(hmiHistory, db_event_first(hContact) != 0);
	return 0;
}

void InitMenus()
{
	{	// Contact menu items
		CMenuItem mi(&g_plugin);
		SET_UID(mi, 0xc20d7a69, 0x7607, 0x4aad, 0xa7, 0x42, 0x10, 0x86, 0xfb, 0x32, 0x49, 0x21);
		mi.name.a = LPGEN("History");
		mi.position = 1000090000;
		mi.pszService = MS_HISTORY_SHOWCONTACTHISTORY;
		mi.hIcolibItem = g_plugin.getIconHandle(IDI_NEWSTORY);
		hmiHistory = Menu_AddContactMenuItem(&mi);
		CreateServiceFunction(mi.pszService, svcShowNewstory);

		HookEvent(ME_CLIST_PREBUILDCONTACTMENU, OnPrebuildContactMenu);

		// Main menu items
		SET_UID(mi, 0x1848519e, 0x639d, 0x497a, 0xa5, 0x37, 0x6b, 0x76, 0x17, 0x2a, 0x6, 0xd9);
		mi.name.a = LPGEN("Global search");
		mi.position = 500060001;
		mi.pszService = "NewStory/GlobalSearch";
		Menu_AddMainMenuItem(&mi);
		CreateServiceFunction(mi.pszService, svcGlobalSearch);
	}

	// Init history item's menu
	CreateServiceFunction("NSMenu/Helper", NSMenuHelper);
	CreateServiceFunction("NSMenu/AddService", NSMenuAddService);
	CreateServiceFunction("NSMenu/ExecService", NSMenuExecService);
	CreateServiceFunction("NSMenu/FreeOwnerData", NSMenuFreeOwnerData);

	hEventPreBuildMenu = CreateHookableEvent(ME_NS_PREBUILDMENU);

	hMenuObject = Menu_AddObject("NSMenu", "NewStory item menu", nullptr, "NSMenu/ExecService");
	Menu_ConfigureObject(hMenuObject, MCO_OPT_USERDEFINEDITEMS, INT_PTR(FALSE));
	Menu_ConfigureObject(hMenuObject, MCO_OPT_FREE_SERVICE, INT_PTR("NSMenu/FreeOwnerData"));
	Menu_ConfigureObject(hMenuObject, MCO_OPT_ONADD_SERVICE, INT_PTR("NSMenu/OnAddService"));

	CMenuItem mi(&g_plugin);
	mi.pszService = "NSMenu/Helper";

	mi.position = 100000;
	mi.name.a = LPGEN("Copy");
	hmiCopy = Menu_AddNewStoryMenuItem(&mi, MENU_COPY);

	mi.position++;
	mi.name.a = LPGEN("Copy text");
	hmiCopyText = Menu_AddNewStoryMenuItem(&mi, MENU_COPYTEXT);

	mi.position++;
	mi.name.a = LPGEN("Copy URL");
	hmiCopyUrl = Menu_AddNewStoryMenuItem(&mi, MENU_COPYURL);

	mi.position++;
	mi.name.a = LPGEN("Show in folder");
	hmiOpenFolder = Menu_AddNewStoryMenuItem(&mi, MENU_OPENFOLDER);

	mi.position++;
	mi.name.a = LPGEN("Quote");
	hmiQuote = Menu_AddNewStoryMenuItem(&mi, MENU_QUOTE);

	mi.position++;
	mi.name.a = LPGEN("Save as");
	hmiSaveAs = Menu_AddNewStoryMenuItem(&mi, MENU_SAVEAS);

	mi.position++;
	mi.name.a = LPGEN("Download");
	hmiDownload = Menu_AddNewStoryMenuItem(&mi, MENU_DOWNLOAD);

	mi.position = 200000;
	mi.name.a = LPGEN("Edit");
	hmiEdit = Menu_AddNewStoryMenuItem(&mi, MENU_EDIT);

	mi.position++;
	mi.name.a = LPGEN("Delete");
	hmiDelete = Menu_AddNewStoryMenuItem(&mi, MENU_DELETE);

	mi.position++;
	mi.name.a = LPGEN("Toggle bookmark");
	hmiBookmark = Menu_AddNewStoryMenuItem(&mi, MENU_BOOKMARK);

	mi.position = 300000;
	mi.name.a = LPGEN("Select all");
	Menu_AddNewStoryMenuItem(&mi, MENU_SELECTALL);
}