1 /* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 module tkmain
is aliced
;
22 import arsd
.simpledisplay
;
29 import iv
.nanovega
.blendish
;
30 import iv
.nanovega
.textlayouter
;
38 version(sfnt_test
) import iv
.nanovega
.simplefont
;
51 // ////////////////////////////////////////////////////////////////////////// //
52 __gshared
bool mainWindowActive
= false;
53 __gshared
bool mainWindowVisible
= false;
54 __gshared SimpleWindow sdmain
;
55 __gshared CList clist
;
58 void setupToxCoreSender () {
59 toxCoreSendEvent
= delegate (Object msg
) {
60 if (msg
is null) return; // just in case
62 if (glconCtlWindow
is null || glconCtlWindow
.closed
) return;
63 glconCtlWindow
.postEvent(msg
);
64 } catch (Exception e
) {}
69 // ////////////////////////////////////////////////////////////////////////// //
70 string
getBrowserCommand (bool forceOpera
=false) {
71 __gshared string browser
;
72 if (forceOpera
) return "opera";
73 if (browser
.length
== 0) {
74 import core
.stdc
.stdlib
: getenv
;
75 const(char)* evar
= getenv("BROWSER");
76 if (evar
!is null && evar
[0]) {
77 import std
.string
: fromStringz
;
78 browser
= evar
.fromStringz
.idup
;
87 void openUrl (ConString url
, bool forceOpera
=false) {
89 import std
.stdio
: File
;
92 auto frd
= File("/dev/null");
93 auto fwr
= File("/dev/null", "w");
94 spawnProcess([getBrowserCommand(forceOpera
), url
.idup
], frd
, fwr
, fwr
, null, Config
.detached
);
95 } catch (Exception e
) {
96 conwriteln("ERROR executing URL viewer (", e
.msg
, ")");
102 // ////////////////////////////////////////////////////////////////////////// //
104 int pos
= -1, len
= 0;
106 @property bool valid () const pure nothrow @safe @nogc => (pos
>= 0 && len
> 0);
107 @property int end () const pure nothrow @safe @nogc => (pos
>= 0 && len
> 0 ? pos
+len
: 0);
109 static UrlInfo
Invalid () pure nothrow @safe @nogc => UrlInfo
.init
;
113 UrlInfo
urlDetect (const(char)[] text
) nothrow @trusted @nogc {
116 auto dlpos
= text
.indexOf("://");
117 if (dlpos
< 3) return res
;
119 //{ import core.stdc.stdio; printf("det: <%.*s>\n", cast(uint)text.length, text.ptr); }
121 bool isProto (const(char)[] prt
) nothrow @trusted @nogc {
122 if (dlpos
< prt
.length
) return false;
123 if (!strEquCI(prt
, text
[dlpos
-prt
.length
..dlpos
])) return false;
124 // check word boundary
125 if (dlpos
== prt
.length
) return true;
126 return !isalpha(text
[dlpos
-prt
.length
-1]);
129 if (isProto("ftp")) res
.pos
= cast(int)(dlpos
-3);
130 else if (isProto("http")) res
.pos
= cast(int)(dlpos
-4);
131 else if (isProto("https")) res
.pos
= cast(int)(dlpos
-5);
134 dlpos
+= 3; // skip "://"
137 for (; dlpos
< text
.length
; ++dlpos
) {
138 char ch
= text
[dlpos
];
139 if (ch
== '/') break;
140 if (!(isalnum(ch
) || ch
== '.' || ch
== '-' || ch
== ':' || ch
== '@')) break;
146 bool wasSharp
= false;
148 for (; dlpos
< text
.length
; ++dlpos
) {
149 char ch
= text
[dlpos
];
158 if (ch
== '(' || ch
== '[' || ch
== '{') {
160 case '(': ch
= ')'; break;
161 case '[': ch
= ']'; break;
162 case '{': ch
= '}'; break;
164 if (brcSP
< brcStack
.length
) brcStack
[brcSP
++] = ch
;
168 if (ch
== ')' || ch
== ']' || ch
== '}') {
169 if (brcSP
== 0 || brcStack
[brcSP
-1] != ch
) break;
175 if (dlpos
== text
.length ||
(!isalnum(text
[dlpos
+1]) && text
[dlpos
+1] != '_')) break;
179 if (ch
<= ' ' || ch
>= 127) break;
182 res
.len
= cast(int)(dlpos
-res
.pos
);
188 // ////////////////////////////////////////////////////////////////////////// //
189 __gshared NVGContext nvg
= null;
190 __gshared NVGImage nvgSkullsImg
;
191 __gshared NVGImage kittyOut
, kittyMsg
, kittyFish
;
192 __gshared NVGImage
[5] statusImgId
;
194 __gshared
int lastWindowWidth
= -1;
197 shared static ~this () {
198 //{ import core.stdc.stdio; printf("******************************\n"); }
199 nvgSkullsImg
.clear();
200 //{ import core.stdc.stdio; printf("---\n"); }
201 foreach (ref img
; statusImgId
[]) img
.clear();
208 void buildStatusImages () {
210 statusImgId
[ContactStatus
.Offline
] = nvg
.createImageRGBA(16, 16, ctiOffline
[], NVGImageFlags
.NoFiltering
);
211 statusImgId
[ContactStatus
.Online
] = nvg
.createImageRGBA(16, 16, ctiOnline
[], NVGImageFlags
.NoFiltering
);
212 statusImgId
[ContactStatus
.Away
] = nvg
.createImageRGBA(16, 16, ctiAway
[], NVGImageFlags
.NoFiltering
);
213 statusImgId
[ContactStatus
.Connecting
] = nvg
.createImageRGBA(16, 16, ctiOffline
[], NVGImageFlags
.NoFiltering
);
215 //{ import core.stdc.stdio; printf("creating status image: Offline\n"); }
216 statusImgId
[ContactStatus
.Offline
] = nvg
.createImageRGBA(16, 16, baph16Gray
[], NVGImageFlags
.NoFiltering
);
217 //{ import core.stdc.stdio; printf("creating status image: Online\n"); }
218 statusImgId
[ContactStatus
.Online
] = nvg
.createImageRGBA(16, 16, baph16Online
[], NVGImageFlags
.NoFiltering
);
219 //{ import core.stdc.stdio; printf("creating status image: Away\n"); }
220 statusImgId
[ContactStatus
.Away
] = nvg
.createImageRGBA(16, 16, baph16Away
[], NVGImageFlags
.NoFiltering
);
221 //{ import core.stdc.stdio; printf("creating status image: Busy\n"); }
222 statusImgId
[ContactStatus
.Busy
] = nvg
.createImageRGBA(16, 16, baph16Busy
[], NVGImageFlags
.NoFiltering
);
223 //{ import core.stdc.stdio; printf("creating status image: Connecting\n"); }
224 statusImgId
[ContactStatus
.Connecting
] = nvg
.createImageRGBA(16, 16, baph16Orange
[], NVGImageFlags
.NoFiltering
);
225 //{ import core.stdc.stdio; printf("+++ creatied status images...\n"); }
226 kittyOut
= nvg
.createImageRGBA(16, 16, kittyOutgoing
[], NVGImageFlags
.NoFiltering
);
227 kittyMsg
= nvg
.createImageRGBA(16, 16, kittyMessage
[], NVGImageFlags
.NoFiltering
);
228 kittyFish
= nvg
.createImageRGBA(16, 16, kittyFish16
[], NVGImageFlags
.NoFiltering
);
233 // ////////////////////////////////////////////////////////////////////////// //
235 nvg
.fonsContext
.addFontsFrom(fstash
);
236 bndSetFont(nvg
.findFont("ui"));
240 // ////////////////////////////////////////////////////////////////////////// //
241 void loadAccount (string nick
, bool allowCreate
) {
242 if (nick
.length
== 0) assert(0, "wtf?!");
248 acc
= new Account(nick
);
249 } catch (Exception e
) {
250 conwriteln("error opening account... (error: ", e
.msg
, ")");
251 if (!allowCreate
) throw e
;
252 acc
= Account
.CreateNew(nick
, nick
);
256 // create fake contact
257 if (newAcc
&& acc
.contacts
.length
== 0 && nick
== "_fakeacc") {
258 conwriteln("creating fake contact...");
259 auto c
= acc
.createEmptyContact();
260 c
.info
.nick
= "test contact";
261 c
.info
.pubkey
[] = 0x55;
265 clist
.buildAccount(acc
);
269 // ////////////////////////////////////////////////////////////////////////// //
271 void doActivateContact (Contact ct
) {
272 if (sdmain
is null || sdmain
.closed
) return;
273 if (activeContact
is ct
) return;
278 //conwriteln("clear log");
279 if (clist
!is null) clist
.resetActiveItem();
280 sdmain
.title
= "BioAcid";
281 glconPostScreenRepaint();
282 } else if (ct
.acceptPending
) {
283 addTextToLog(ct
.acc
, ct
, LogFile
.Msg
.Kind
.Notification
, false, (ct
.statusmsg
.length ? ct
.statusmsg
: "I brought you a tasty fish!"), systimeNow
);
285 import std
.format
: format
;
286 sdmain
.title
= "%s [%s] -- BioAcid".format(ct
.info
.nick
, tox_hex(ct
.info
.pubkey
));
289 auto mcount
= cast(int)log
.messages
.length
;
290 int left
= ct
.hmcOnOpen
;
291 if (left
< ct
.unreadCount
) left
= ct
.unreadCount
;
292 if (left
> mcount
) left
= mcount
;
293 if (mcount
> left
) mcount
= left
;
295 foreach (const ref msg
; log
.messages
[$-mcount
..$]) {
296 if (left
== ct
.unreadCount
) addDividerLine();
297 addTextToLog(ct
.acc
, ct
, msg
);
301 if (ct
.unreadCount
!= 0) { ct
.unreadCount
= 0; ct
.saveUnreadCount(); }
308 //FIXME: scan all accounts
309 void fixTrayIcon () {
310 if (clist
is null) return;
311 auto acc
= clist
.mainAccount
;
312 if (acc
is null) return;
314 foreach (Contact ct
; acc
) unc
+= ct
.unreadCount
;
316 import std
.format
: format
;
318 setHint("unread: %d".format(unc
));
320 setTrayStatus(acc
.status
);
321 final switch (acc
.status
) {
322 case ContactStatus
.Connecting
: setHint("connecting..."); break;
323 case ContactStatus
.Offline
: setHint("offline"); break;
324 case ContactStatus
.Online
: setHint("online"); break;
325 case ContactStatus
.Away
: setHint("away"); break;
326 case ContactStatus
.Busy
: setHint("busy"); break;
332 void fixUnreadIndicators () {
333 if (!mainWindowVisible ||
!mainWindowActive
) return; // nothing to do
334 if (activeContact
is null || activeContact
.unreadCount
== 0) return; // nothing to do
335 activeContact
.unreadCount
= 0;
336 activeContact
.unreadCount
= 0;
337 activeContact
.saveUnreadCount();
342 // ////////////////////////////////////////////////////////////////////////// //
343 void addContactCommands () {
344 conRegFunc
!((ConString grpname
) {
345 if (clist
is null || sdmain
is null || sdmain
.closed
) return;
346 auto acc
= clist
.mainAccount
;
347 if (acc
is null) return;
348 if (grpname
.length
== 0) { conwriteln("group name?"); return; }
349 if (acc
.findGroupByName(grpname
) != uint.max
) { conwriteln("group <", grpname
, "> already exists"); return; }
350 auto gid
= acc
.createGroup(grpname
);
351 if (gid
== uint.max
) { conwriteln("cannot create group <", grpname
, ">"); return; }
352 clist
.buildAccount(acc
);
353 glconPostScreenRepaint();
354 })("group_create", "create group");
357 conRegFunc
!((ConString grpname
) {
358 if (clist
is null || sdmain
is null || sdmain
.closed
) return;
359 if (activeContact
is null) { conwriteln("please, select contact first"); return; }
360 if (grpname
.length
== 0) { conwriteln("group name?"); return; }
361 auto gid
= activeContact
.acc
.createGroup(grpname
);
362 if (gid
== uint.max
) { conwriteln("cannot create group <", grpname
, ">"); return; }
363 if (activeContact
.acc
.moveContactToGroup(activeContact
, gid
)) {
364 clist
.buildAccount(activeContact
.acc
);
365 glconPostScreenRepaint();
367 conwriteln("cannot move contact to new group");
369 })("move_to_group", "move current contact to named group");
373 if (clist
is null || sdmain
is null || sdmain
.closed
) return;
374 if (activeContact
is null) { conwriteln("please, select contact first"); return; }
375 auto acc
= activeContact
.acc
;
376 if (!acc
.isOnline
) { conwriteln("you must be online to remove contacts"); return; }
377 if (!acc
.removeContact(activeContact
)) { conwriteln("cannot remove current contact"); return; }
379 activeContact
= null;
380 clist
.buildAccount(acc
);
381 glconPostScreenRepaint();
382 })("contact_remove", "unfriend and remove current contact");