summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_options.cpp
blob: 719d30cbc5a5fb3271ecbc7f3a69c3302afd563e (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
#include "common.h"

INT_PTR CALLBACK CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CToxProto *proto = (CToxProto*)GetWindowLongPtr(hwnd, GWLP_USERDATA);

	switch (uMsg)
	{
	case WM_INITDIALOG:
		TranslateDialogDefault(hwnd);
		{
			proto = (CToxProto*)lParam;
			SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);

			ptrA username(proto->getStringA("Username"));
			SetDlgItemTextA(hwnd, IDC_USERNAME, username);

			std::string toxProfilePath = proto->GetToxProfilePath();
			SetDlgItemTextA(hwnd, IDC_DATAPATH, toxProfilePath.c_str());

			ptrW groupName(proto->getTStringA(TOX_SETTINGS_GROUP));
			SetDlgItemText(hwnd, IDC_GROUP, groupName);
			SendDlgItemMessage(hwnd, IDC_GROUP, EM_LIMITTEXT, 64, 0);

			CheckDlgButton(hwnd, IDC_DISABLE_UDP, proto->getByte("DisableUDP", 0));
			CheckDlgButton(hwnd, IDC_DISABLE_IPV6, proto->getByte("DisableIPv6", 0));

			if (proto->IsOnline())
			{
				EnableWindow(GetDlgItem(hwnd, IDC_USERNAME), FALSE);
				EnableWindow(GetDlgItem(hwnd, IDC_DATAPATH), FALSE);
				EnableWindow(GetDlgItem(hwnd, IDC_DISABLE_UDP), FALSE);
				EnableWindow(GetDlgItem(hwnd, IDC_DISABLE_IPV6), FALSE);
			}
		}
		return TRUE;

	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case IDC_USERNAME:
			if ((HWND)lParam == GetFocus())
			{
				EnableWindow(GetDlgItem(hwnd, IDC_USERNAME), !proto->IsOnline());
				if (HIWORD(wParam) != EN_CHANGE) return 0;
				char username[128];
				GetDlgItemTextA(hwnd, IDC_USERNAME, username, SIZEOF(username));
				SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			}
			break;

		case IDC_DATAPATH:
			if ((HWND)lParam == GetFocus())
			{
				EnableWindow(GetDlgItem(hwnd, IDC_DATAPATH), !proto->IsOnline());
				if (HIWORD(wParam) != EN_CHANGE) return 0;
				char dataPath[128];
				GetDlgItemTextA(hwnd, IDC_DATAPATH, dataPath, SIZEOF(dataPath));
				SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			}
			break;

		case IDC_BROWSE:
			{
				char dataPath[MAX_PATH];
				GetDlgItemTextA(hwnd, IDC_DATAPATH, dataPath, SIZEOF(dataPath));

				char filter[MAX_PATH] = "";
				mir_snprintf(filter, MAX_PATH, "%s\0*.*\0", Translate("All Files (*.*)"));

				OPENFILENAMEA ofn = { 0 };
				ofn.lStructSize = sizeof(ofn);
				ofn.hwndOwner = 0;
				ofn.lpstrFilter = filter;
				ofn.nFilterIndex = 1;
				ofn.lpstrFile = strrchr(dataPath, '\\') + 1;
				ofn.lpstrTitle = Translate("Select data file");
				ofn.nMaxFile = SIZEOF(dataPath);
				ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_NOCHANGEDIR;
				if (GetOpenFileNameA(&ofn) && dataPath[0])
				{
					SetDlgItemTextA(hwnd, IDC_DATAPATH, dataPath);
					SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
				}
			}
			break;

		case IDC_GROUP:
			{
				if ((HIWORD(wParam) != EN_CHANGE || (HWND)lParam != GetFocus()))
					return 0;
				SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			}
			break;

		case IDC_DISABLE_UDP:
			SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			break;

		case IDC_DISABLE_IPV6:
			SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
			break;
		}
	}
		break;

	case WM_NOTIFY:
		if (reinterpret_cast<NMHDR*>(lParam)->code == PSN_APPLY)
		{
			if (!proto->IsOnline())
			{
				char username[128];
				GetDlgItemTextA(hwnd, IDC_USERNAME, username, SIZEOF(username));
				proto->setString("Username", username);
				tox_set_name(proto->tox, (uint8_t*)&username[0], strlen(username));

				proto->UninitToxCore();

				char dataPath[128];
				GetDlgItemTextA(hwnd, IDC_DATAPATH, dataPath, SIZEOF(dataPath));
				proto->setString("DataPath", dataPath);

				proto->setByte("DisableUDP", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_UDP));
				proto->setByte("DisableIPv6", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_IPV6));

				proto->InitToxCore();
			}

			wchar_t groupName[128];
			GetDlgItemText(hwnd, IDC_GROUP, groupName, SIZEOF(groupName));
			if (lstrlen(groupName) > 0)
			{
				proto->setWString(NULL, TOX_SETTINGS_GROUP, groupName);
				Clist_CreateGroup(0, groupName);
			}
			else
				proto->delSetting(NULL, TOX_SETTINGS_GROUP);

			return TRUE;
		}
		break;
	}

	return FALSE;
}