summaryrefslogtreecommitdiff
path: root/plugins/Sessions/Src/SaveSessions.cpp
blob: c843e7ee6e60b778735683b4d9f7b699977eee6d (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
/*
Copyright (C) 2012-24 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"

HWND g_hSDlg;

bool bSC = false;

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

static int SaveUserSessionName(MCONTACT *contacts, wchar_t *szUSessionName)
{
	if (contacts[0] == 0)
		return 1;

	CSession *pSession = nullptr;
	for (auto &it : g_arUserSessions)
		if (!it->wszName.CompareNoCase(szUSessionName))
			pSession = it;

	if (pSession)
		pSession->contacts.clear();
	else {
		g_plugin.g_lastUserId = g_plugin.g_lastUserId + 1;

		pSession = new CSession();
		pSession->id = g_plugin.g_lastUserId;
		pSession->bIsUser = true;
		pSession->wszName = szUSessionName;
		g_arUserSessions.insert(pSession);
	}

	for (int i = 0; contacts[i]; i++)
		pSession->contacts.push_back(contacts[i]);

	pSession->save();

	while (g_arUserSessions.getCount() > g_plugin.iTrackCount) {
		g_arUserSessions[0].remove();
		g_arUserSessions.remove(int(0));
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Save session dialog

class CSessionDlg : public CDlgBase
{
	CCtrlClc m_clist;
	CCtrlCombo m_sessions;
	CCtrlCheck chkSaveAndClose;
	CCtrlMButton chkSelContacts;

	MCONTACT user_session_list[255];

public:
	CSessionDlg() : 
		CDlgBase(g_plugin, IDD_SAVEDIALOG),
		m_clist(this, IDC_CLIST),
		m_sessions(this, IDC_LIST),
		chkSelContacts(this, IDC_SELCONTACTS, SKINICON_AUTH_ADD, LPGEN("More contacts")),
		chkSaveAndClose(this, IDC_SANDCCHECK)
	{
		memset(user_session_list, 0, sizeof(user_session_list));

		chkSelContacts.OnClick = Callback(this, &CSessionDlg::onChange_SelContacts);

		m_clist.OnCheckChanged = Callback(this, &CSessionDlg::onCheckChanged_Clist);
	}

	bool OnInitDialog() override
	{
		chkSelContacts.MakeFlat();
		chkSelContacts.MakePush();

		g_hSDlg = m_hwnd;
		LoadSessionToCombobox(m_sessions, true);

		SetWindowLongPtr(m_clist.GetHwnd(), GWL_STYLE,
			GetWindowLongPtr(m_clist.GetHwnd(), GWL_STYLE) | CLS_CHECKBOXES | CLS_HIDEEMPTYGROUPS | CLS_USEGROUPS | CLS_GREYALTERNATE | CLS_GROUPCHECKBOXES);
		m_clist.SetExStyle(CLS_EX_DISABLEDRAGDROP | CLS_EX_TRACKSELECT);
		m_clist.AutoRebuild();

		LoadPosition(m_hwnd, "SaveDlg");
		return true;
	}

	bool OnApply() override
	{
		wchar_t szUserSessionName[MAX_PATH];
		m_sessions.GetText(szUserSessionName, _countof(szUserSessionName));
		rtrimw(szUserSessionName);
		if (szUserSessionName[0] == 0) {
			MessageBox(nullptr, TranslateT("Session name is empty, enter the name and try again"), TranslateT("Sessions Manager"), MB_OK | MB_ICONWARNING);
			return false;
		}

		if (chkSelContacts.IsPushed() && bSC) {
			int i = 0;
			for (auto &hContact : Contacts()) {
				uint8_t res = m_clist.GetCheck(m_clist.FindContact(hContact));
				if (res) {
					user_session_list[i] = hContact;
					i++;
				}
			}

			SaveUserSessionName(user_session_list, szUserSessionName);
			return true;
		}

		if (!SaveUserSessionName(session_list, szUserSessionName)) {
			if (chkSaveAndClose.IsChecked())
				CloseCurrentSession(0, 0);
			return true;
		}

		MessageBox(nullptr, TranslateT("Current session is empty!"), TranslateT("Sessions Manager"), MB_OK | MB_ICONWARNING);
		return false;
	}

	void OnDestroy() override
	{
		SavePosition(m_hwnd, "SaveDlg");
		g_hSDlg = nullptr;
	}

	void onCheckChanged_Clist(CCtrlClc*)
	{
		bSC = TRUE;
		memcpy(user_session_list, session_list, sizeof(user_session_list));
	}

	void onChange_SelContacts(CCtrlCheck *)
	{
		if (!m_bInitialized)
			return;

		RECT rWnd;
		GetWindowRect(m_hwnd, &rWnd);

		int x = rWnd.right - rWnd.left, y = rWnd.bottom - rWnd.top, dy, dx, dd;

		if (chkSelContacts.IsPushed()) {
			chkSaveAndClose.Disable();
			dy = 20;
			dx = 150;
			dd = 5;
			m_clist.Show();
		}
		else {
			chkSaveAndClose.Enable();
			dy = -20;
			dx = -150;
			dd = 5;
			m_clist.Hide();
		}

		SetWindowPos(m_hwnd, nullptr, rWnd.left, rWnd.top, x + dx, y + (dx / 3), SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE);
		SetWindowPos(m_clist.GetHwnd(), nullptr, x - dd, dd, dx - dd, y + (dx / 12), SWP_NOZORDER);

		for (int i = 0; session_list[i] > 0; i++)
			m_clist.SetCheck(m_clist.FindContact(session_list[i]), 1);

		OffsetWindow(m_hwnd, GetDlgItem(m_hwnd, IDC_LIST), 0, dy);
		OffsetWindow(m_hwnd, GetDlgItem(m_hwnd, IDC_STATIC), 0, dy);
		OffsetWindow(m_hwnd, GetDlgItem(m_hwnd, IDC_SANDCCHECK), 0, dy);
		OffsetWindow(m_hwnd, GetDlgItem(m_hwnd, IDOK), 0, dy);
		OffsetWindow(m_hwnd, GetDlgItem(m_hwnd, IDCANCEL), 0, dy);
	}
};

INT_PTR SaveUserSessionHandles(WPARAM, LPARAM)
{
	if (g_hSDlg) {
		ShowWindow(g_hSDlg, SW_SHOW);
		return 1;
	}

	(new CSessionDlg())->Show();
	return 0;
}