1 module ircclient.ui.settingsdlg;
2 
3 import dlangui.core.stdaction;
4 import dlangui.dialogs.dialog;
5 import dlangui.widgets.widget;
6 import dlangui.dml.parser;
7 import ircclient.ui.frame;
8 import ircclient.ui.settings;
9 import std.utf : toUTF32;
10 import std.conv : to;
11 
12 class SettingsDialog : Dialog {
13     IRCFrame _frame;
14     IRCSettings _settings;
15     bool _allowConnect;
16     this(IRCFrame parent, IRCSettings settings, bool allowConnect) {
17         super(UIString.fromRaw("IRC Client Settings"d), parent.window,
18               DialogFlag.Modal | DialogFlag.Resizable | DialogFlag.Popup, 500, 400);
19         _icon = "dlangui-logo1";
20         _frame = parent;
21         _settings = settings;
22         _allowConnect = allowConnect;
23     }
24 
25     /// override to implement creation of dialog controls
26     override void initialize() {
27         super.initialize();
28         Widget content;
29         try {
30             content = parseML(q{
31                 VerticalLayout {
32                     id: vlayout
33                     padding: Rect { 5, 5, 5, 5 }
34                     layoutWidth: fill; layoutHeight: fill
35                     TableLayout {
36                         margins: 5
37                         colCount: 2
38                         layoutWidth: fill; layoutHeight: wrap
39 
40                         TextWidget { text: "IRC Server host name" }
41                         EditLine { id: edHost; layoutWidth: fill; minWidth: 400 }
42                         TextWidget { text: "IRC Server port" }
43                         EditLine { id: edPort; layoutWidth: fill }
44                         TextWidget { text: " " }
45                         TextWidget { text: " " }
46                         TextWidget { text: "Nickname" }
47                         EditLine { id: edNick; layoutWidth: fill }
48                         TextWidget { text: "Alternate nickname" }
49                         EditLine { id: edAlternateNick; layoutWidth: fill }
50                         TextWidget { text: "Username" }
51                         EditLine { id: edUsername; layoutWidth: fill }
52                         TextWidget { text: "Real name" }
53                         EditLine { id: edRealName; layoutWidth: fill }
54                         TextWidget { text: " " }
55                         TextWidget { text: " " }
56                         TextWidget { text: "Channel to join on connect" }
57                         EditLine { id: edChannel; layoutWidth: fill }
58                         TextWidget { text: " " }
59                         CheckBox { id: cbCreateWorkspace; text: "Connect on startup"; checked: true }
60                     }
61                     TextWidget { id: statusText; text: ""; layoutWidth: fill }
62                 }
63             });
64         } catch (Exception e) {
65             Log.e("Exceptin while parsing DML", e);
66             throw e;
67         }
68         addChild(content);
69         addChild(createButtonsPanel(_allowConnect ? [ACTION_CONNECT, ACTION_APPLY, ACTION_CANCEL] : [ACTION_APPLY, ACTION_CANCEL], 0, 0));
70         settingsToControls();
71     }
72 
73     void settingsToControls() {
74         childById("edHost").text = toUTF32(_settings.host);
75         childById("edPort").text = toUTF32(to!string(_settings.port));
76         childById("edNick").text = toUTF32(_settings.nick);
77         childById("edAlternateNick").text = toUTF32(_settings.alternateNick);
78         childById("edUsername").text = toUTF32(_settings.userName);
79         childById("edRealName").text = toUTF32(_settings.userRealName);
80         childById("edChannel").text = toUTF32(_settings.defChannel);
81     }
82 
83     void controlsToSettings() {
84         _settings.host = toUTF8(childById("edHost").text);
85         try {
86             _settings.port = cast(ushort)to!ulong(childById("edPort").text);
87         } catch (Exception e) {
88             // ignore
89             _settings.port = 6667;
90         }
91         _settings.nick = toUTF8(childById("edNick").text);
92         _settings.alternateNick = toUTF8(childById("edAlternateNick").text);
93         _settings.userName = toUTF8(childById("edUsername").text);
94         _settings.userRealName = toUTF8(childById("edRealName").text);
95         _settings.defChannel = toUTF8(childById("edChannel").text);
96     }
97 
98     override void close(const Action action) {
99         Action newaction = action.clone();
100         if (action.id != ACTION_CANCEL.id) {
101             controlsToSettings();
102             // TODO: validate
103         }
104         //if (action.id == IDEActions.FileNewWorkspace || action.id == IDEActions.FileNewProject) {
105         //    newaction.objectParam = _result;
106         //}
107         super.close(newaction);
108     }
109 }