277 lines
9.8 KiB
C
277 lines
9.8 KiB
C
/*
|
||
* gui.c
|
||
*
|
||
* Created on: 6 июл. 2022 г.
|
||
* Author: alexander
|
||
*/
|
||
|
||
#include <string.h>
|
||
#include <stdlib.h>
|
||
#include <stdio.h>
|
||
|
||
#include "gui.h"
|
||
#include "settings.h"
|
||
#include "node_settings.h"
|
||
#include "images.h"
|
||
#include "arguments.h"
|
||
#include "address.h"
|
||
#include "keylang.h"
|
||
|
||
#include "db.h"
|
||
#include "xrdp.h"
|
||
#include "../xfreerdp.h"
|
||
|
||
static int guiExit(Ihandle *self)
|
||
{
|
||
return IUP_CLOSE;
|
||
}
|
||
|
||
int isEmptyFieldServerUser(Ihandle *self)
|
||
{
|
||
char *name = IupGetAttribute(self, "NAME");
|
||
if (!strcmp(name, "SERVER"))
|
||
return !strcmp(IupGetAttribute(IupGetDialogChild(self, "USER"), "LINEVALUE"), "\0");
|
||
else
|
||
return !strcmp(IupGetAttribute(IupGetDialogChild(self, "SERVER"), "VALUE"), "\0");
|
||
}
|
||
|
||
static int activeBtnConnect(Ihandle *self, int c, char *value)
|
||
{
|
||
if (!strcmp(value, "\0") || isEmptyFieldServerUser(self))
|
||
{
|
||
IupSetInt(IupGetDialogChild(self, "CONNECT"), "ACTIVE", 0);
|
||
#ifdef IUP_WITH_COLOR_BUTTONS
|
||
IupSetAttribute(IupGetDialogChild(self, "CONNECT"), "BGCOLOR", "49 54 61");
|
||
IupSetAttribute(IupGetDialogChild(self, "CONNECT"), "FGCOLOR", "238 238 238");
|
||
#endif
|
||
}
|
||
else
|
||
{
|
||
IupSetInt(IupGetDialogChild(self, "CONNECT"), "ACTIVE", 1);
|
||
#ifdef IUP_WITH_COLOR_BUTTONS
|
||
IupSetAttribute(IupGetDialogChild(self, "CONNECT"), "BGCOLOR", "0 179 0");
|
||
IupSetAttribute(IupGetDialogChild(self, "CONNECT"), "FGCOLOR", "255 255 255");
|
||
#endif
|
||
}
|
||
return IUP_DEFAULT;
|
||
}
|
||
|
||
static int guiConnect(Ihandle *self)
|
||
{
|
||
char *host = IupGetAttribute(IupGetDialogChild(self, "SERVER"), "VALUE");
|
||
char *result = getHostIP(host);
|
||
|
||
if (!result)
|
||
return IUP_IGNORE;
|
||
|
||
char *login = IupGetAttribute(IupGetDialogChild(self, "USER"), "LINEVALUE");
|
||
|
||
if (!strcmp(login, "\0"))
|
||
return IUP_IGNORE;
|
||
|
||
setParameterValue(PARAMETER_SERVER, VALUE_SERVER, result);
|
||
setParameterValue(PARAMETER_USERNAME, VALUE_USERNAME, login);
|
||
setParameterValue(PARAMETER_PASSWORD, VALUE_PASSWORD, IupGetAttribute(IupGetDialogChild(self, "PASSWORD"), "LINEVALUE"));
|
||
|
||
Arguments args;
|
||
buildArguments(&args);
|
||
for (int i = 0; i < args.argc; ++i)
|
||
{
|
||
printf("%s\n", args.argv[i]);
|
||
}
|
||
|
||
enum XF_EXIT_CODE xCode = free_rdp_connect(args.argc, args.argv);
|
||
|
||
if (!(xCode == XF_EXIT_DNS_NAME_NOT_FOUND || xCode == XF_EXIT_CONNECT_FAILED || xCode == XF_EXIT_TLS_CONNECT_FAILED))
|
||
{
|
||
IupSetAttribute(IupGetDialogChild(self, "PASSWORD"), "VALUE", "");
|
||
|
||
if (!dbAddServer(result, host))
|
||
{
|
||
dbInsertHistory(login, host);
|
||
IupSetAttribute(IupGetDialogChild(self, "SERVER"), "APPENDITEM", host);
|
||
}
|
||
dbSetUserNameCurrent(login);
|
||
}
|
||
|
||
freeArguments(&args);
|
||
|
||
return IUP_DEFAULT;
|
||
}
|
||
|
||
static void createHostsList(Ihandle *iupList)
|
||
{
|
||
Hosts *hosts = dbGetHostsList();
|
||
char *indexItem = NULL;
|
||
char *serverName = NULL;
|
||
|
||
for (size_t i = 0; i < hosts->size; ++i)
|
||
{
|
||
Host *host = hosts->host[i];
|
||
size_t size = strlen(host->data[0]) + 1;
|
||
indexItem = (char *)malloc(sizeof(char) * size);
|
||
strncpy(indexItem, host->data[0], size);
|
||
size = strlen(host->data[1]) + 1;
|
||
serverName = (char *)malloc(sizeof(char) * size);
|
||
strncpy(serverName, host->data[1], size);
|
||
|
||
IupSetAttribute(iupList, indexItem, serverName);
|
||
if (atoi(host->data[2]))
|
||
IupSetAttribute(iupList, "VALUE", serverName);
|
||
}
|
||
dbFreeHosts(hosts);
|
||
}
|
||
|
||
#ifdef IUP_WITH_LANGUAGE_KEYBOARD_ICON
|
||
static int k_any(Ihandle *self, int key)
|
||
{
|
||
int langcode = getKeyboardLanguage();
|
||
if (langcode)
|
||
{
|
||
if (langcode == 1)
|
||
IupSetAttribute(IupGetDialogChild(self, "KEYBOARD"), "IMAGE", "keyboardUs");
|
||
else if (langcode == 2)
|
||
IupSetAttribute(IupGetDialogChild(self, "KEYBOARD"), "IMAGE", "keyboardRu");
|
||
else
|
||
IupSetAttribute(IupGetDialogChild(self, "KEYBOARD"), "IMAGE", "keyboardEmpty");
|
||
}
|
||
return IUP_DEFAULT;
|
||
}
|
||
#endif
|
||
|
||
Ihandle* guiStart()
|
||
{
|
||
Ihandle *dlg;
|
||
Ihandle *icon, *bgHead;
|
||
Ihandle *vBoxMain, *hBoxServer, *hBoxUser, *hBoxPassword, *hBoxButtons; // Boxes
|
||
Ihandle *labelImage;
|
||
Ihandle *labelServer, *inputServer;
|
||
Ihandle *labelUser, *inputUser;
|
||
Ihandle *labelPassword, *inputPassword;
|
||
Ihandle *btnConnect, *btnClose, *btnSettings;
|
||
|
||
icon = IupImageRGBA(64, 64, idata_icon);
|
||
IupSetHandle("icon", icon);
|
||
|
||
labelImage = IupLabel(NULL);
|
||
|
||
bgHead = IupImageRGBA(405, 72, idata_head);
|
||
IupSetAttributeHandle(labelImage, "IMAGE", bgHead);
|
||
|
||
labelServer = IupLabel("Компьютер:");
|
||
IupSetAttribute(labelServer, "SIZE", "80x16");
|
||
IupSetAttribute(labelServer, "ALIGNMENT", "ARIGHT:ACENTER");
|
||
IupSetAttribute(labelServer, "PADDING", "5");
|
||
|
||
inputServer = IupList(NULL);
|
||
IupSetAttribute(inputServer, "NAME", "SERVER");
|
||
IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера\n(обязательное для заполнения поле)");
|
||
createHostsList(inputServer);
|
||
IupSetAttribute(inputServer, "SIZE", "100");
|
||
IupSetAttribute(inputServer, "EDITBOX", "YES");
|
||
IupSetAttribute(inputServer, "DROPDOWN", "YES");
|
||
IupSetAttribute(inputServer, "MASK", "[A-Za-z0-9/./-]*");
|
||
IupSetCallback(inputServer, "EDIT_CB", (Icallback) activeBtnConnect);
|
||
hBoxServer = IupHbox(labelServer, inputServer, NULL);
|
||
IupSetAttribute(hBoxServer, "MARGIN", "10x10");
|
||
|
||
labelUser = IupLabel("Пользователь:");
|
||
IupSetAttribute(labelUser, "SIZE", "80x16");
|
||
IupSetAttribute(labelUser, "ALIGNMENT", "ARIGHT:ACENTER");
|
||
IupSetAttribute(labelUser, "PADDING", "5");
|
||
inputUser = IupText(NULL);
|
||
IupSetAttribute(inputUser, "NAME", "USER");
|
||
IupSetAttribute(inputUser, "TIP", "<домен>\\<имя пользователя>\n<имя пользователя>@<домен>\n(обязательное для заполнения поле)");
|
||
IupSetAttribute(inputUser, "VALUE", getSetValueCurrent(PARAMETER_USERNAME));
|
||
IupSetAttribute(inputUser, "SIZE", "100");
|
||
IupSetAttribute(inputUser, "MASK", "(/w|[/./\\/@/-])*");
|
||
IupSetCallback(inputUser, "ACTION", (Icallback) activeBtnConnect);
|
||
hBoxUser = IupHbox(labelUser, inputUser, NULL);
|
||
IupSetAttribute(hBoxUser, "MARGIN", "10x0");
|
||
|
||
labelPassword = IupLabel("Пароль:");
|
||
IupSetAttribute(labelPassword, "SIZE", "80x16");
|
||
IupSetAttribute(labelPassword, "ALIGNMENT", "ARIGHT:ACENTER");
|
||
IupSetAttribute(labelPassword, "PADDING", "5");
|
||
inputPassword = IupText(NULL);
|
||
IupSetAttribute(inputPassword, "SIZE", "100");
|
||
IupSetAttribute(inputPassword, "NAME", "PASSWORD");
|
||
IupSetAttribute(inputPassword, "TIP", "Пароль пользователя (необязательно)");
|
||
IupSetAttribute(inputPassword, "VALUE", getSetValueCurrent(PARAMETER_PASSWORD));
|
||
IupSetAttribute(inputPassword, "PASSWORD", "YES");
|
||
hBoxPassword = IupHbox(labelPassword, inputPassword, NULL);
|
||
IupSetAttribute(hBoxPassword, "MARGIN", "10x10");
|
||
|
||
#ifdef IUP_WITH_LANGUAGE_KEYBOARD_ICON
|
||
Ihandle *keyboardRu, *keyboardUs, *keyboardEmpty, *iconLang;
|
||
keyboardEmpty = IupImageRGBA(32, 32, empty_keyboard);
|
||
IupSetHandle("keyboardEmpty", keyboardEmpty);
|
||
keyboardRu = IupImageRGBA(32, 32, ru_keyboard);
|
||
IupSetHandle("keyboardRu", keyboardRu);
|
||
keyboardUs = IupImageRGBA(32, 32, us_keyboard);
|
||
IupSetHandle("keyboardUs", keyboardUs);
|
||
iconLang = IupLabel(NULL);
|
||
IupSetAttribute(iconLang, "NAME", "KEYBOARD");
|
||
IupSetAttribute(iconLang, "IMAGE", "keyboardRu");
|
||
IupSetAttribute(iconLang, "FLOATING", "YES");
|
||
IupSetAttribute(iconLang, "POSITION", "13,222");
|
||
IupSetAttribute(iconLang, "TIP", "Текущая раскладка клавиатуры (Alt+Shift)");
|
||
#endif
|
||
|
||
btnConnect = IupButton("Подключение", NULL);
|
||
IupSetHandle("CONNECT", btnConnect);
|
||
IupSetAttribute(btnConnect, "NAME", "CONNECT");
|
||
IupSetAttribute(btnConnect, "TIP", "Выполнить подключение");
|
||
#ifdef IUP_WITH_COLOR_BUTTONS
|
||
IupSetAttribute(btnConnect, "BGCOLOR", "0 179 0");
|
||
IupSetAttribute(btnConnect, "FGCOLOR", "255 255 255");
|
||
#endif
|
||
|
||
btnClose = IupButton("Закрыть", NULL);
|
||
IupSetHandle("CLOSE", btnClose);
|
||
IupSetAttribute(btnClose, "NAME", "CLOSE");
|
||
IupSetAttribute(btnClose, "TIP", "Закрыть FreeRDP");
|
||
#ifdef IUP_WITH_COLOR_BUTTONS
|
||
IupSetAttribute(btnClose, "BGCOLOR", "204 0 0");
|
||
IupSetAttribute(btnClose, "FGCOLOR", "255 255 255");
|
||
#endif
|
||
|
||
btnSettings = IupButton("Настройки", NULL);
|
||
IupSetAttribute(btnSettings, "NAME", "SETTINGS");
|
||
IupSetAttribute(btnSettings, "TIP", "Перейти в настройки");
|
||
#ifdef IUP_WITH_COLOR_BUTTONS
|
||
IupSetAttribute(btnSettings, "BGCOLOR", "0 136 204");
|
||
IupSetAttribute(btnSettings, "FGCOLOR", "255 255 255");
|
||
#endif
|
||
|
||
hBoxButtons = IupHbox(IupFill(), btnSettings, btnConnect, btnClose, IupFill(), NULL);
|
||
IupSetAttribute(hBoxButtons, "ALIGNMENT", "ACENTER:ACENTER");
|
||
IupSetAttribute(hBoxButtons, "GAP", "10");
|
||
IupSetAttribute(hBoxButtons, "MARGIN", "10x10");
|
||
|
||
vBoxMain = IupVbox(
|
||
#ifdef IUP_WITH_LANGUAGE_KEYBOARD_ICON
|
||
iconLang,
|
||
#endif
|
||
labelImage, hBoxServer, hBoxUser, hBoxPassword, hBoxButtons, NULL);
|
||
|
||
IupSetCallback(btnConnect, "ACTION", (Icallback) guiConnect);
|
||
IupSetCallback(btnClose, "ACTION", (Icallback) guiExit);
|
||
IupSetCallback(btnSettings, "ACTION", (Icallback) settingsMainWindow);
|
||
|
||
dlg = IupDialog(vBoxMain);
|
||
IupSetAttribute(dlg, "IMAGE", "BG_HEAD");
|
||
IupSetAttribute(dlg, "ICON", "icon");
|
||
IupSetAttribute(dlg, "TITLE", "GUI FreeRDP");
|
||
IupSetAttribute(dlg, "RESIZE", "NO");
|
||
IupSetAttribute(dlg, "PARENTDIALOG", "MAIN_WINDOW");
|
||
IupSetAttribute(dlg, "DEFAULTENTER", "CONNECT");
|
||
IupSetAttribute(dlg, "DEFAULTESC", "CLOSE");
|
||
#ifdef IUP_WITH_LANGUAGE_KEYBOARD_ICON
|
||
IupSetCallback(dlg, "iup_XkeyAlt(K_LSHIFT)", (Icallback) k_any);
|
||
IupSetCallback(dlg, "GETFOCUS_CB", (Icallback) k_any);
|
||
#endif
|
||
|
||
return dlg;
|
||
}
|