From 0ac4b544972fb011e7c7c69e60a1d5d180ada0ac Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Sat, 7 Sep 2013 11:57:39 +0000
Subject: MS_CLIST_GROUPEXISTS - new service to detect the clist group's
 presence

git-svn-id: http://svn.miranda-ng.org/main/trunk@5993 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 src/modules/clist/groups.cpp | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)

(limited to 'src')

diff --git a/src/modules/clist/groups.cpp b/src/modules/clist/groups.cpp
index fe0a7b6ffb..d99ed04a09 100644
--- a/src/modules/clist/groups.cpp
+++ b/src/modules/clist/groups.cpp
@@ -65,17 +65,25 @@ static int GroupNameExists(const TCHAR *name, int skipGroup)
 	return 0;
 }
 
-static INT_PTR CreateGroup(WPARAM wParam, LPARAM lParam)
+static INT_PTR GroupExists(WPARAM, LPARAM lParam)
+{
+	if (lParam == 0)
+		return FALSE;
+
+	return GroupNameExists((LPCTSTR)lParam, -1);
+}
+
+static INT_PTR CreateGroupInternal(INT_PTR iParent, const TCHAR *ptszName)
 {
 	int newId = CountGroups();
 	TCHAR newBaseName[127], newName[128];
 	char str[33];
 	int i;
-	DBVARIANT dbv;
 
-	const TCHAR* grpName = lParam ? (TCHAR*)lParam : TranslateT("New Group");
-	if (wParam) {
-		_itoa(wParam - 1, str, 10);
+	const TCHAR* grpName = ptszName ? ptszName : TranslateT("New Group");
+	if (iParent) {
+		_itoa(iParent - 1, str, 10);
+		DBVARIANT dbv;
 		if (db_get_ts(NULL, "CListGroups", str, &dbv))
 			return 0;
 
@@ -86,7 +94,7 @@ static INT_PTR CreateGroup(WPARAM wParam, LPARAM lParam)
 
 	_itoa(newId, str, 10);
 	lstrcpyn(newName + 1, newBaseName, SIZEOF(newName) - 1);
-	if (lParam) {
+	if (ptszName) {
 		i = GroupNameExists(newBaseName, -1);
 		if (i) newId = i - 1;
 		i = !i;
@@ -109,6 +117,26 @@ static INT_PTR CreateGroup(WPARAM wParam, LPARAM lParam)
 	return newId + 1;
 }
 
+static INT_PTR CreateGroup(WPARAM wParam, LPARAM lParam)
+{
+	if (lParam == 0)
+		return CreateGroupInternal(wParam, NULL);
+
+	LPCTSTR ptszName = (LPCTSTR)lParam;
+	if (ptszName == NULL || ptszName[0] == '\0' || ptszName[0] == '\\')
+		return 0;
+
+	TCHAR *tszName = NEWTSTR_ALLOCA(ptszName);
+	for (TCHAR *p = tszName; *p; p++) {
+		if (*p == '\\') {
+			*p = '\0';
+			CreateGroupInternal(wParam, tszName);
+			*p = '\\';
+		}
+	}
+	return CreateGroupInternal(wParam, tszName);
+}
+
 static INT_PTR GetGroupName2(WPARAM wParam, LPARAM lParam)
 {
 	char idstr[33];
@@ -552,6 +580,7 @@ int InitGroupServices(void)
 		db_free(&dbv);
 	}
 
+	CreateServiceFunction(MS_CLIST_GROUPEXISTS, GroupExists);
 	CreateServiceFunction(MS_CLIST_GROUPCREATE, CreateGroup);
 	CreateServiceFunction(MS_CLIST_GROUPDELETE, DeleteGroup);
 	CreateServiceFunction(MS_CLIST_GROUPRENAME, RenameGroup);
-- 
cgit v1.2.3