INT_PTR CALLBACK EditSettingDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
	{
		char tmp[32];
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lParam);
		switch (((struct DBsetting*)lParam)->dbv.type)
		{
		case DBVT_BYTE:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			if (!((struct DBsetting*)lParam)->setting[0])
			{
				SetWindowText(hwnd, Translate("New BYTE value"));
			}
			else
			{
				SetWindowText(hwnd, Translate("Edit BYTE value"));
				SetDlgItemText(hwnd, IDC_SETTINGNAME, ((struct DBsetting*)lParam)->setting);
				SetDlgItemText(hwnd, IDC_SETTINGVALUE, itoa(((struct DBsetting*)lParam)->dbv.bVal, tmp, 10));
			}
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, CHK_DECIMAL);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_BYTE);
			break;
		case DBVT_WORD:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			if (!((struct DBsetting*)lParam)->setting[0])
			{
				SetWindowText(hwnd, Translate("New WORD value"));
			}
			else
			{
				SetWindowText(hwnd, Translate("Edit WORD value"));
				SetDlgItemText(hwnd, IDC_SETTINGNAME, ((struct DBsetting*)lParam)->setting);
				SetDlgItemText(hwnd, IDC_SETTINGVALUE, itoa(((struct DBsetting*)lParam)->dbv.wVal, tmp, 10));
			}
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, CHK_DECIMAL);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_WORD);
			break;
		case DBVT_DWORD:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			if (!((struct DBsetting*)lParam)->setting[0])
			{
				SetWindowText(hwnd, Translate("New DWORD value"));
			}
			else
			{
				char text[32];
				SetWindowText(hwnd, Translate("Edit DWORD value"));
				SetDlgItemText(hwnd, IDC_SETTINGNAME, ((struct DBsetting*)lParam)->setting);
				mir_snprintf(text, SIZEOF(text), "%X", ((struct DBsetting*)lParam)->dbv.dVal);
				SetDlgItemText(hwnd, IDC_SETTINGVALUE, text);
			}
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, CHK_HEX);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, CHK_DWORD);
			break;
		case DBVT_ASCIIZ:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_SHOW);
			ShowWindow(GetDlgItem(hwnd, IDC_SETTINGVALUE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_HEX), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DECIMAL), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_BASE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_TYPE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_BYTE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_WORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DWORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_STRING), SW_HIDE);
			if (!((struct DBsetting*)lParam)->setting[0])
			{
				SetWindowText(hwnd, Translate("New STRING value"));
			}
			else
			{
				SetWindowText(hwnd, Translate("Edit STRING value"));
				SetDlgItemText(hwnd, IDC_SETTINGNAME, ((struct DBsetting*)lParam)->setting);
				SetDlgItemText(hwnd, IDC_STRING, ((struct DBsetting*)lParam)->dbv.pszVal);
			}
			break;
		case DBVT_UTF8:
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_SHOW);
			ShowWindow(GetDlgItem(hwnd, IDC_SETTINGVALUE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_HEX), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DECIMAL), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_BASE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_TYPE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_BYTE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_WORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DWORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_STRING), SW_HIDE);
			if (!((struct DBsetting*)lParam)->setting[0])
			{
				SetWindowText(hwnd, Translate("New UNICODE value"));
			}
			else
			{
				char *tmp = (((struct DBsetting*)lParam)->dbv.pszVal);
				int length = (int)strlen(tmp) + 1;
				WCHAR *wc = (WCHAR*)_alloca(length*sizeof(WCHAR));
				MultiByteToWideChar(CP_UTF8, 0, tmp, -1, wc, length);
				SetDlgItemTextW(hwnd, IDC_STRING, wc);

				SetWindowText(hwnd, Translate("Edit UNICODE value"));
				SetDlgItemText(hwnd, IDC_SETTINGNAME, ((struct DBsetting*)lParam)->setting);
			}
			break;
		case DBVT_BLOB:
		{
			ShowWindow(GetDlgItem(hwnd, IDC_STRING), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, IDC_SETTINGVALUE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, IDC_BLOB), SW_SHOW);

			if (!((struct DBsetting*)lParam)->setting[0])
			{
				SetWindowText(hwnd, Translate("New BLOB value"));
			}
			else
			{
				int j;
				char tmp[16];
				int len = ((struct DBsetting*)lParam)->dbv.cpbVal;
				char *data = (char*)_alloca(3 * (len + 1) + 10);
				BYTE *p = ((struct DBsetting*)lParam)->dbv.pbVal;

				if (!data) return TRUE;
				data[0] = '\0';

				for (j = 0; j < len; j++)
				{
					mir_snprintf(tmp, SIZEOF(tmp), "%02X ", (BYTE)p[j]);
					strcat(data, tmp);
				}

				SetWindowText(hwnd, Translate("Edit BLOB value"));
				SetDlgItemText(hwnd, IDC_SETTINGNAME, ((struct DBsetting*)lParam)->setting);
				SetDlgItemText(hwnd, IDC_BLOB, data);
			}
			ShowWindow(GetDlgItem(hwnd, CHK_HEX), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DECIMAL), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_BASE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, GRP_TYPE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_BYTE), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_WORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_DWORD), SW_HIDE);
			ShowWindow(GetDlgItem(hwnd, CHK_STRING), SW_HIDE);
		}
		break;
		default: return TRUE;
		}
		TranslateDialogDefault(hwnd);
	}
	return TRUE;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case CHK_BYTE:
		case CHK_WORD:
		case CHK_DWORD:
			EnableWindow(GetDlgItem(hwnd, CHK_HEX), 1);
			EnableWindow(GetDlgItem(hwnd, CHK_DECIMAL), 1);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, LOWORD(wParam));
			break;
		case CHK_STRING:
			EnableWindow(GetDlgItem(hwnd, CHK_HEX), 0);
			EnableWindow(GetDlgItem(hwnd, CHK_DECIMAL), 0);
			CheckRadioButton(hwnd, CHK_BYTE, CHK_STRING, LOWORD(wParam));
			break;

		case CHK_HEX:
		case CHK_DECIMAL:
			CheckRadioButton(hwnd, CHK_HEX, CHK_DECIMAL, LOWORD(wParam));
			{
				char *setting, temp[32];
				int settingLength, tmp;
				settingLength = GetWindowTextLength(GetDlgItem(hwnd, IDC_SETTINGVALUE));
				if (settingLength)
				{
					setting = (char*)_alloca(settingLength + 1);
					if (setting)
					{
						// havta convert it with mir_snprintf()
						GetDlgItemText(hwnd, IDC_SETTINGVALUE, setting, settingLength + 1);
						if (LOWORD(wParam) == CHK_DECIMAL && IsDlgButtonChecked(hwnd, CHK_DECIMAL))
						{
							sscanf(setting, "%X", &tmp);
							mir_snprintf(temp, SIZEOF(temp), "%ld", tmp);
						}
						else
						{
							sscanf(setting, "%d", &tmp);
							mir_snprintf(temp, SIZEOF(temp), "%X", tmp);
						}
						SetDlgItemText(hwnd, IDC_SETTINGVALUE, temp);
					}
				}
			}
			break;
		case IDOK:
		{
			struct DBsetting *dbsetting = (struct DBsetting*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
			char *setting, *value;
			int settingLength, valueLength, valueID = IDC_SETTINGVALUE;
			settingLength = GetWindowTextLength(GetDlgItem(hwnd, IDC_SETTINGNAME));

			if (IsWindowVisible(GetDlgItem(hwnd, IDC_STRING)))
				valueID = IDC_STRING;
			else
				if (IsWindowVisible(GetDlgItem(hwnd, IDC_SETTINGVALUE)))
					valueID = IDC_SETTINGVALUE;
				else
					if (IsWindowVisible(GetDlgItem(hwnd, IDC_BLOB)))
						valueID = IDC_BLOB;
					else
						break;

			valueLength = GetWindowTextLength(GetDlgItem(hwnd, valueID));

			if (dbsetting->dbv.type == DBVT_UTF8)
				valueLength *= sizeof(WCHAR);

			if (settingLength)
			{
				int settingValue;
				setting = (char*)_alloca(settingLength + 1);

				if (valueLength)
					value = (char*)_alloca(valueLength + 2);
				else
					value = (char*)_alloca(2);

				if (!setting || !value)
				{
					msg(Translate("Couldn't allocate enough memory!"), modFullname);
					DestroyWindow(hwnd);
					break;
				}

				GetDlgItemText(hwnd, IDC_SETTINGNAME, setting, settingLength + 1);

				if (valueLength)
				{
					if (dbsetting->dbv.type == DBVT_UTF8)
						GetDlgItemTextW(hwnd, valueID, (LPWSTR)value, (valueLength + 2));
					else
						GetDlgItemText(hwnd, valueID, value, valueLength + 1);
				}
				else
					if (IsWindowVisible(GetDlgItem(hwnd, IDC_STRING)) || (saveAsType(hwnd) == 3))
						memcpy(value, "\0\0", 2);
					else
						strcpy(value, "0");

				// delete the old setting
				if (mir_strcmp(setting, dbsetting->setting) && dbsetting->setting && (dbsetting->setting)[0] != 0)
					db_unset(dbsetting->hContact, dbsetting->module, dbsetting->setting);

				// delete the setting if we are saving as a different type
				switch (dbsetting->dbv.type)
				{
				case DBVT_BYTE:
					if (saveAsType(hwnd) != 0) db_unset(dbsetting->hContact, dbsetting->module, setting);
					break;
				case DBVT_WORD:
					if (saveAsType(hwnd) != 1) db_unset(dbsetting->hContact, dbsetting->module, setting);
					break;
				case DBVT_DWORD:
					if (saveAsType(hwnd) != 2) db_unset(dbsetting->hContact, dbsetting->module, setting);
					break;
					//case DBVT_ASCIIZ:
					//db_set_s(dbsetting->hContact, dbsetting->module, setting, value);
					//break;
				}
				// write the setting
				switch (saveAsType(hwnd))
				{
				case 0:
					if (IsDlgButtonChecked(hwnd, CHK_HEX)) sscanf(value, "%x", &settingValue);
					else sscanf(value, "%d", &settingValue);
					db_set_b(dbsetting->hContact, dbsetting->module, setting, (BYTE)settingValue);
					break;
				case 1:
					if (IsDlgButtonChecked(hwnd, CHK_HEX)) sscanf(value, "%x", &settingValue);
					else sscanf(value, "%d", &settingValue);
					db_set_w(dbsetting->hContact, dbsetting->module, setting, (WORD)settingValue);
					break;
				case 2:
					if (IsDlgButtonChecked(hwnd, CHK_HEX)) sscanf(value, "%x", &settingValue);
					else sscanf(value, "%d", &settingValue);
					db_set_dw(dbsetting->hContact, dbsetting->module, setting, (DWORD)settingValue);
					break;
				case 3:
					if (dbsetting->dbv.type == DBVT_UTF8)
						db_set_ws(dbsetting->hContact, dbsetting->module, setting, (WCHAR*)value);
					else if (dbsetting->dbv.type == DBVT_BLOB)
						WriteBlobFromString(dbsetting->hContact, dbsetting->module, setting, value, valueLength);
					else if (dbsetting->dbv.type == DBVT_ASCIIZ)
						db_set_s(dbsetting->hContact, dbsetting->module, setting, value);
					break;
				}

			}
		} // fall through
		case IDCANCEL:
		{
			struct DBsetting *dbsetting = (struct DBsetting*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
			mir_free(dbsetting->module);
			mir_free(dbsetting->setting);
			mir_free(dbsetting);
			DestroyWindow(hwnd);
		}
		break;
		}
		break;
	}
	return 0;
}
Example #2
0
void importSettings(HANDLE hContact, char *importstring )
{
	char module[256] = "", setting[256] = "", *end;
	int i=0, value, type;
	importstring = strtok(importstring, "\n");

	SetCursor(LoadCursor(NULL,IDC_WAIT));

	while (importstring != NULL)
	{
		i=0;
		rtrim(importstring);
		if (importstring[i] == '\0')
		{
			importstring = strtok(NULL, "\n");
			continue;
		}
		else if (!strncmp(&importstring[i],"SETTINGS:",strlen("SETTINGS:")))
		{
			importstring = strtok(NULL, "\n");
			continue;
		}
		else if (!strncmp(&importstring[i],"CONTACT:", strlen("CONTACT:")))
		{
			int len, add = 1;
			hContact = INVALID_HANDLE_VALUE;

			i = i + (int)strlen("CONTACT:");
			len = (int)strlen(&importstring[i]);

			if (len > 10)
			{
				char uid[256]="",szUID[256]="",szProto[512]="";
				char *p1,*p2;

				p1 = strrchr(&importstring[i], '>*{');
				p2 = strrchr(&importstring[i], '}*');

				if (p1 && p2 && p1+3 < p2 && p2-p1 < SIZEOF(szUID))
				{
					strncpy(szUID, p1+1, p2-p1-2);

					p1 = strrchr(&importstring[i], ')*<');
					p2 = strrchr(&importstring[i], '>*{');

					if (p1 && p2 && p1+3 < p2 && p2-p1 < SIZEOF(uid))
					{
						strncpy(uid, p1+1, p2-p1-3);

						p1 = strrchr(&importstring[i], ' *(');
						p2 = strrchr(&importstring[i], ')*<');

						if (p1 && p2 && p1+3 < p2 && p2-p1 < SIZEOF(szProto))
						{
							char *protouid;
							strncpy(szProto, p1+1, p2-p1-3);

							protouid = (char*)CallProtoService(szProto,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0);
							if ((int)protouid!=CALLSERVICE_NOTFOUND)
							{
								if (!mir_strcmp(protouid, uid))
        							hContact = CheckNewContact(szProto, uid, szUID);
        					}
	        				else
        						hContact = CheckNewContact(szProto, uid, szUID);
						}
					}
				}
			}

 			if (hContact == INVALID_HANDLE_VALUE)
 			{
				HANDLE temp = (HANDLE)CallService(MS_DB_CONTACT_ADD,0,0);
				if (temp)
					hContact = temp;
			}
		}
		else if (importstring[i] == '[' && !strchr(&importstring[i+1],'=') )// get the module
		{
			if (end = strpbrk(&importstring[i+1], "]")) {
				if ((end+1) != '\0') *end = '\0';
				strcpy(module, &importstring[i+1]);
			}
		}
		else if (importstring[i] == '-' && importstring[i+1] == '[' &&
			!strchr(&importstring[i+2],'='))// get the module
		{
			if (end = strpbrk(&importstring[i+2], "]")) {
				if ((end+1) != '\0') *end = '\0';
				strcpy(module, &importstring[i+2]);
				deleteModule(module, hContact, 1);
			}
		}
		else if (strstr(&importstring[i], "=") && module[0]) // get the setting
		{
			if (end = strpbrk(&importstring[i+1], "=")) {
				if ((end+1) != '\0') *end = '\0';
				strcpy(setting, &importstring[i]);

				// get the type
				type = *(end+1);
				if (lstrcmp(module, "CList") == 0 && lstrcmp(setting, "Group") == 0)
				{
					WCHAR* GroupName = mir_a2u(end+2);
					if (!GroupName)
						continue;
					HANDLE GroupHandle = Clist_GroupExists(GroupName);
					if(GroupHandle == 0) {
						GroupHandle = (HANDLE)CallService(MS_CLIST_GROUPCREATE, 0, (LPARAM)GroupName);

						if(GroupHandle) {
							CallService(MS_CLUI_GROUPADDED, (WPARAM)GroupHandle, 0);
							CallService(MS_CLIST_GROUPSETEXPANDED, (WPARAM)GroupHandle, 1);
						}
					}
					mir_free(GroupName);
				}
				switch (type)
				{
					case 'b':
					case 'B':
						if (sscanf((end+2), "%d", &value) == 1)
							DBWriteContactSettingByte(hContact, module, setting, (BYTE)value);
						break;
					case 'w':
					case 'W':
						if (sscanf((end+2), "%d", &value) == 1)
							DBWriteContactSettingWord(hContact, module, setting, (WORD)value);
						break;
					case 'd':
					case 'D':
						if (sscanf((end+2), "%d", &value) == 1)
							DBWriteContactSettingDword(hContact, module, setting, (DWORD)value);
						break;
					case 's':
					case 'S':
						DBWriteContactSettingString(hContact,module, setting, (end+2));
						break;
					case 'g':
					case 'G':
						{	char *pstr;
							for(pstr=end+2;*pstr;pstr++){
								if(*pstr=='\\'){
									switch(pstr[1]){
									case 'n': *pstr='\n'; break;
									case 't': *pstr='\t'; break;
									case 'r': *pstr='\r'; break;
									default:  *pstr=pstr[1]; break;
									}
									MoveMemory(pstr+1,pstr+2,lstrlenA(pstr+2)+1);
						}	}	}
					case 'u':
					case 'U':
						DBWriteContactSettingStringUtf(hContact,module, setting, (end+2));
						break;
					case 'l':
					case 'L':
						DBDeleteContactSetting(hContact, module, setting);
						break;
					case 'n':
					case 'N':
						WriteBlobFromString(hContact, module, setting, (end+2), (int)strlen((end+2)));
						break;
				}
			}
		}
		importstring = strtok(NULL, "\n");
	}
	SetCursor(LoadCursor(NULL,IDC_ARROW));
}