summaryrefslogtreecommitdiff
path: root/dbeditorpp/exportimport.c
diff options
context:
space:
mode:
Diffstat (limited to 'dbeditorpp/exportimport.c')
-rw-r--r--dbeditorpp/exportimport.c676
1 files changed, 676 insertions, 0 deletions
diff --git a/dbeditorpp/exportimport.c b/dbeditorpp/exportimport.c
new file mode 100644
index 0000000..b398b77
--- /dev/null
+++ b/dbeditorpp/exportimport.c
@@ -0,0 +1,676 @@
+#include "headers.h"
+
+int Openfile(char *outputFile, const char *module)
+{
+ OPENFILENAME ofn = {0};
+ char filename[MAX_PATH] = "";
+ char *filter = "INI Files\0*.ini\0All Files\0*.*\0";
+ int r;
+ char *title = Translate("Export to file");
+
+ if (module)
+ {
+ int n = 0;
+ mir_strncpy(filename, module, MAX_PATH);
+
+ while(filename[n])
+ {
+ switch(filename[n])
+ {
+ case '*':
+ case ':':
+ case '/':
+ case '?':
+ case '|':
+ case '\\':
+ filename[n] = '_';
+ break;
+ }
+ n++;
+ }
+ }
+
+ ofn.lStructSize = sizeof(ofn);
+ ofn.lpstrFile = filename;
+ ofn.lpstrFilter = filter;
+ ofn.Flags = OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
+ ofn.lpstrTitle = title;
+ ofn.nMaxFile = MAX_PATH;
+ ofn.lpstrDefExt = "ini";
+
+ r = GetSaveFileName(&ofn);
+ if (!r)
+ return 0;
+ lstrcpy(outputFile,filename);
+ return 1;
+}
+
+void exportModule(HANDLE hContact, char* module, FILE* file)
+{
+ char tmp[32];
+ ModuleSettingLL settinglist;
+ struct ModSetLinkLinkItem *setting;
+
+ if (IsModuleEmpty(hContact,module)) return;
+ EnumSettings(hContact,module,&settinglist);
+
+ // print the module header..
+ fprintf(file, "[%s]\n", module);
+ setting = settinglist.first;
+ while(setting)
+ {
+ DBVARIANT dbv;
+ if (!GetSetting(hContact, module, setting->name, &dbv))
+ {
+ switch (dbv.type)
+ {
+ case DBVT_BYTE:
+ fprintf(file, "%s=b%s\n", setting->name, itoa(dbv.bVal,tmp,10));
+ break;
+ case DBVT_WORD:
+ fprintf(file, "%s=w%s\n", setting->name, itoa(dbv.wVal,tmp,10));
+ break;
+ case DBVT_DWORD:
+ fprintf(file, "%s=d%s\n", setting->name, itoa(dbv.dVal,tmp,10));
+ break;
+ case DBVT_ASCIIZ:
+ fprintf(file, "%s=s%s\n", setting->name, dbv.pszVal);
+ break;
+ case DBVT_UTF8:
+ fprintf(file, "%s=u%s\n", setting->name, dbv.pszVal);
+ /*{
+ //convert \r to STX and \n to ETX
+ char *end = strchr(dbv.pszVal, '\r');
+ while (end) // convert \r to STX
+ {
+ *end = 2;
+ end = strchr(end+1, '\r');
+ }
+ end = strchr(dbv.pszVal, '\n');
+ while (end) // convert \n to ETX
+ {
+ *end = 3;
+ end = strchr(end+1, '\n');
+ }
+
+ if (dbv.type == DBVT_UTF8)
+ fprintf(file, "%s=u%s\n", setting->name, dbv.pszVal);
+ else
+ fprintf(file, "%s=s%s\n", setting->name, dbv.pszVal);
+ }*/
+ break;
+ case DBVT_BLOB:
+ {
+ int j;
+ char *data = NULL;
+ if (!(data = (char*)malloc( 3*(dbv.cpbVal+1)) ))
+ break;
+ data[0] = '\0';
+ for (j=0; j<dbv.cpbVal; j++)
+ {
+ char tmp[16];
+ _snprintf(tmp, 16, "%02X ", (BYTE)dbv.pbVal[j]);
+ strcat(data, tmp);
+ }
+ fprintf(file,"%s=n%s\n",setting->name , data);
+ safe_free(data);
+ }
+ break;
+ }
+ }
+ DBFreeVariant(&dbv);
+ setting = (struct ModSetLinkLinkItem *)setting->next;
+ }
+ fprintf(file, "\n");
+ FreeModuleSettingLL(&settinglist);
+
+}
+
+
+static char *NickFromHContact(HANDLE hContact)
+{
+ static char nick[512] = "";
+
+ if (hContact)
+ {
+ char szProto[256];
+ int loaded = 0;
+
+ if (GetValue(hContact,"Protocol","p",szProto,sizeof(szProto)))
+ loaded = IsProtocolLoaded(szProto);
+
+ if (!szProto[0] || !loaded)
+ {
+ char name[256];
+
+ if (szProto[0])
+ {
+ if (GetValue(hContact,szProto,"Nick",name,sizeof(name)))
+ mir_snprintf(nick, sizeof(nick),"%s (%s)", name, szProto);
+ else
+ mir_snprintf(nick, sizeof(nick),"(UNKNOWN) (%s)", szProto);
+ }
+ else
+ mir_snprintf(nick, sizeof(nick),"(UNKNOWN)");
+ }
+ else
+ {
+ char *uid;
+ char szUID[256];
+
+ uid = (char*)CallProtoService(szProto,PS_GETCAPS,PFLAG_UNIQUEIDSETTING,0);
+ if ((int)uid!=CALLSERVICE_NOTFOUND && uid)
+ {
+ GetValue(hContact, szProto, uid, szUID, sizeof(szUID));
+ mir_snprintf(nick, sizeof(nick), "%s *(%s)*<%s>*{%s}*", (char*)GetContactName(hContact,szProto,0), szProto, uid, szUID);
+ }
+ else
+ mir_snprintf(nick, sizeof(nick), "%s (%s)", (char*)GetContactName(hContact,szProto,0), szProto);
+ }
+ }
+
+ return nick;
+}
+
+
+void exportDB(HANDLE hContact, char* module) // hContact == -1 export entire db. module == NULL export entire contact.
+{ // hContact == -1, module == "" - all contacts
+ FILE* file = NULL;
+ char fileName[MAX_PATH];
+ int nullcontactDone = 0;
+ ModuleSettingLL modlist;
+ struct ModSetLinkLinkItem *mod;
+
+ // enum all the modules
+ if (!EnumModules(&modlist)) { msg(Translate("Error Loading Module List"),modFullname); return;}
+
+ if (Openfile(fileName, ((int)hContact==-1)?NULL:module))
+ {
+ if (!(file = fopen(fileName, "wt"))) { msg(Translate("Couldn't open file for writing"), modFullname); return; }
+
+ SetCursor(LoadCursor(NULL,IDC_WAIT));
+
+ // exporting entire db
+ if (hContact == INVALID_HANDLE_VALUE)
+ {
+ hContact = NULL;
+
+ if (module == NULL)
+ {
+ fprintf(file, "SETTINGS:\n\n");
+ mod = modlist.first;
+ while(mod) // null contact first
+ {
+ exportModule(hContact, mod->name, file);
+ mod = (struct ModSetLinkLinkItem *)mod->next;
+ }
+ fprintf(file, "\n");
+
+ }
+ else
+ {
+ if (module == "") module = NULL; // reset module for all contacts export
+ }
+
+ hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
+
+ while (hContact)
+ {
+ if (!hContact) continue;
+
+ // filter
+ if (Mode != MODE_ALL)
+ {
+ char szProto[256];
+ int loaded = 0;
+
+ if (GetValue(hContact,"Protocol","p",szProto,sizeof(szProto)))
+ loaded = IsProtocolLoaded(szProto);
+
+ if ((loaded && Mode == MODE_UNLOADED) || (!loaded && Mode == MODE_LOADED))
+ {
+ hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0);
+ continue;
+ }
+ }
+
+ fprintf(file, "CONTACT: %s\n\n", NickFromHContact(hContact));
+
+ if (module == NULL) // export all modules
+ {
+ mod = modlist.first;
+ while(mod)
+ {
+ exportModule(hContact, mod->name, file);
+ mod = (struct ModSetLinkLinkItem *)mod->next;
+ }
+ }
+ else // export module
+ {
+ exportModule(hContact, module, file);
+ }
+
+ fprintf(file, "\n");
+ hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)(HANDLE)hContact, 0);
+ }
+ }
+ // exporting a contact
+ else
+ {
+ if (!module) // exporting every module
+ {
+ if (hContact)
+ fprintf(file, "CONTACT: %s\n\n", NickFromHContact(hContact));
+ else
+ fprintf(file, "SETTINGS:\n\n");
+
+ mod = modlist.first;
+ while(mod)
+ {
+ exportModule(hContact, mod->name, file);
+ mod = (struct ModSetLinkLinkItem *)mod->next;
+ }
+ }
+ else
+ {
+ if (hContact)
+ fprintf(file, "FROM CONTACT: %s\n\n", NickFromHContact(hContact));
+ else
+ fprintf(file, "SETTINGS:\n\n");
+
+ exportModule(hContact, module, file);
+ }
+
+ fprintf(file, "\n");
+ }
+ fclose(file);
+
+ SetCursor(LoadCursor(NULL,IDC_ARROW));
+ }
+
+ FreeModuleSettingLL(&modlist);
+
+}
+
+
+HANDLE CheckNewContact(char *myProto, char *uid, char *myName)
+{
+ char szProto[256], szName[256];
+ HANDLE resultHandle = INVALID_HANDLE_VALUE;
+ HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
+
+ while (hContact)
+ {
+ //szProto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
+ if (DBGetContactSettingStringStatic(hContact, "Protocol", "p", szProto, 256))
+ {
+ if (!mir_strcmp(szProto, myProto))
+ {
+ if (GetValue(hContact, szProto, uid, szName, sizeof(szName)) &&
+ !mir_strcmp(szName, myName))
+ {
+ //char msg[1024];
+ //_snprintf(msg, 1024, Translate("Do you want to overwrite it \"%s\"?"), szName);
+ //if (MessageBox(0,msg, Translate("Contact already exists"), MB_YESNO|MB_ICONEXCLAMATION) == IDYES)
+ resultHandle = hContact;
+ break;
+ }
+ }
+ }
+
+ hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0);
+ }
+
+ return resultHandle;
+
+}
+
+
+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;
+ if (importstring[i] == '\n')
+ {
+ importstring = strtok(NULL, "\n");
+ continue;
+ }
+ else if (!strncmp(&importstring[i],"SETTINGS:",strlen("SETTINGS:")))
+ {
+ hContact = 0;
+/*
+ end = strstr(&importstring[i], "\n");
+ if (end)
+ {
+ i = end - &importstring[i];
+ }
+*/
+ }
+ else if (!strncmp(&importstring[i],"CONTACT:", strlen("CONTACT:")))
+ {
+
+ int len, add = 1;
+
+ hContact = INVALID_HANDLE_VALUE;
+
+ //end = strstr(&importstring[i], "\n");
+
+ i = i + strlen("CONTACT:");
+ len = 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;
+ }
+/*
+ if (end)
+ {
+ i = end - &importstring[i];
+ }
+*/
+ }
+ 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],'=') &&
+ hContact != INVALID_HANDLE_VALUE)// 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] &&
+ hContact != INVALID_HANDLE_VALUE) // 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);
+ 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 'u':
+ DBWriteContactSettingString(hContact,module, setting, (end+2));
+ break;
+ case 'S':
+ case 'U':
+ DBWriteContactSettingStringUtf(hContact,module, setting, (end+2));
+ /*{
+ char *string;
+ string = (end+2);
+ end = strchr(string, '\r');
+ if (end) // remove \r
+ *end = 0;
+ end = strchr(string, 2);
+ while (end) // convert STX back to \r
+ {
+ *end = '\r';
+ end = strchr(++end, 2);
+ }
+ end = strchr(string, 3);
+ while (end) // convert ETX back to \n
+ {
+ *end = '\n';
+ end = strchr(++end, 3);
+ }
+ if (type == 'u' || type == 'U')
+ DBWriteContactSettingStringUtf(hContact,module, setting, string);
+ else
+ DBWriteContactSettingString(hContact,module, setting, string);
+ }*/
+ break;
+ case 'l':
+ case 'L':
+ DBDeleteContactSetting(hContact, module, setting);
+ break;
+ case 'n':
+ case 'N':
+ WriteBlobFromString(hContact,module,setting,(end+2),strlen((end+2)));
+ break;
+ }
+ }
+ }
+ importstring = strtok(NULL, "\n");
+ }
+
+ SetCursor(LoadCursor(NULL,IDC_ARROW));
+}
+
+
+#define crlf_string "\x02\x03\0"
+
+BOOL CALLBACK ImportDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ if (msg == WM_INITDIALOG)
+ {
+ hwnd2importWindow = hwnd;
+ SetWindowLong(hwnd,GWL_USERDATA,lParam);
+ TranslateDialogDefault(hwnd);
+ SendDlgItemMessage(hwnd, IDC_TEXT, EM_LIMITTEXT, (WPARAM)sizeof(TCHAR)*0x7FFFFFFF, 0);
+ }
+ else
+ if (msg == WM_COMMAND)
+ {
+ switch(LOWORD(wParam))
+ {
+ case IDC_CRLF:
+ {
+ int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
+ char *string = _alloca(length+4);
+ int Pos = 2;
+
+ if (length)
+ {
+ int Range = SendDlgItemMessageA(hwnd,IDC_TEXT,EM_GETSEL,0,0);
+ int Min = LOWORD(Range);
+ int Max = HIWORD(Range);
+
+
+ GetDlgItemText(hwnd, IDC_TEXT, string, length+1);
+
+ if (Min == -1)
+ memcpy(string, crlf_string, 3);
+ else
+ if (Max == -1 || Max >= length)
+ memcpy(&string[Min], crlf_string, 3);
+ else
+ if (Max-Min > 2)
+ {
+ memcpy(&string[Min], crlf_string, 2);
+ memmove(&string[Min+2], &string[Max], length - Max + 1);
+ }
+ else
+ {
+ memmove(&string[Min+2], &string[Max], length - Max + 1);
+ memcpy(&string[Min], crlf_string, 2);
+ }
+
+ if (Min) Pos += Min;
+ }
+ else
+ memcpy(string, crlf_string, 3);
+
+ SetDlgItemText(hwnd, IDC_TEXT, string);
+ SendDlgItemMessageA(hwnd,IDC_TEXT,EM_SETSEL,Pos,Pos);
+ SetFocus(GetDlgItem(hwnd, IDC_TEXT));
+ }
+ break;
+ case IDOK:
+ {
+ HANDLE hContact = (HANDLE)GetWindowLong(hwnd,GWL_USERDATA);
+ int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
+ char *string;
+ if (length)
+ {
+ string = (char*)_alloca(length+1);
+ if (!string) {msg(Translate("Couldnt allocate enough memory!"), modFullname); DestroyWindow(hwnd); }
+ GetDlgItemText(hwnd, IDC_TEXT, string, length+1);
+ importSettings(hContact, string);
+ refreshTree(1);
+ }
+ }
+ // fall through
+ case IDCANCEL:
+ DestroyWindow(hwnd);
+ hwnd2importWindow = 0;
+ break;
+ }
+ }
+ return 0;
+}
+
+void ImportSettingsMenuItem(HANDLE hContact)
+{
+ if (hwnd2importWindow)
+ DestroyWindow(hwnd2importWindow);
+
+ CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_IMPORT), 0,ImportDlgProc, (LPARAM)hContact);
+}
+
+int Openfile2Import(char *outputFiles)
+{
+ OPENFILENAME ofn = {0};
+ char *filter = "INI Files\0*.ini\0All Files\0*.*\0";
+ char *title = Translate("Import from files");
+
+ ofn.lStructSize = sizeof(ofn);
+ ofn.lpstrFile = outputFiles;
+ ofn.lpstrFilter = filter;
+ ofn.Flags = OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
+ ofn.lpstrTitle = title;
+ ofn.nMaxFile = MAX_PATH*10;
+
+ if (!GetOpenFileName(&ofn))
+ return 0;
+
+ return ofn.nFileOffset;
+}
+
+void ImportSettingsFromFileMenuItem(HANDLE hContact)
+{
+ char szFileNames[MAX_PATH*10] = {0};
+ char szPath[MAX_PATH] = "";
+ char szFile[MAX_PATH];
+ DWORD offset = Openfile2Import(szFileNames);
+ int index = 0;
+ HANDLE hFile, hMap;
+ PBYTE pFile = NULL;
+
+ if (offset)
+ {
+ if (strlen(szFileNames) < offset)
+ {
+ index += offset;
+ strncpy(szPath, szFileNames, offset);
+ strcat(szPath, "\\");
+ }
+
+ while(szFileNames[index])
+ {
+ strcpy(szFile, szPath);
+ strcat(szFile, &szFileNames[index]);
+ index += strlen(&szFileNames[index])+1;
+
+ hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+ if (hFile != INVALID_HANDLE_VALUE)
+ {
+ if (GetFileSize(hFile, NULL) > 0)
+ {
+ hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
+
+ if (hMap) {
+ pFile = MapViewOfFile(hMap, FILE_MAP_COPY, 0, 0 ,0);
+
+ if (pFile) {
+ importSettings(hContact, pFile);
+ UnmapViewOfFile(pFile);
+ }
+ CloseHandle(hMap);
+ }
+
+ }
+ CloseHandle(hFile);
+ }
+ else
+ break;
+
+ }
+
+ refreshTree(1);
+ }
+
+}