fixed lastseen text (cosmetix)
[bioacid.git] / tkmain.d
blob316c006bd62d110c11bfbcb2336c00f84aa7de01
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, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module tkmain is aliced;
19 import std.datetime;
21 import arsd.color;
22 import arsd.image;
23 import arsd.simpledisplay;
25 import iv.cmdcon;
26 import iv.cmdcon.gl;
27 import iv.gxx;
28 import iv.meta;
29 import iv.nanovega;
30 import iv.nanovega.blendish;
31 import iv.nanovega.textlayouter;
32 import iv.strex;
33 import iv.tox;
34 import iv.sdpyutil;
35 import iv.unarray;
36 import iv.utfutil;
37 import iv.vfs.io;
39 version(sfnt_test) import iv.nanovega.simplefont;
41 import accdb;
42 import accobj;
43 import fonts;
44 import icondata;
45 import notifyicon;
46 import toxproto;
48 import tkclist;
49 import tklog;
52 // ////////////////////////////////////////////////////////////////////////// //
53 __gshared bool mainWindowActive = false;
54 __gshared bool mainWindowVisible = false;
55 __gshared SimpleWindow sdmain;
56 __gshared CList clist;
59 void setupToxCoreSender () {
60 toxCoreSendEvent = delegate (Object msg) {
61 if (msg is null) return; // just in case
62 try {
63 if (glconCtlWindow is null || glconCtlWindow.closed) return;
64 glconCtlWindow.postEvent(msg);
65 } catch (Exception e) {}
70 // ////////////////////////////////////////////////////////////////////////// //
71 string getBrowserCommand (bool forceOpera=false) {
72 __gshared string browser;
73 if (forceOpera) return "opera";
74 if (browser.length == 0) {
75 import core.stdc.stdlib : getenv;
76 const(char)* evar = getenv("BROWSER");
77 if (evar !is null && evar[0]) {
78 import std.string : fromStringz;
79 browser = evar.fromStringz.idup;
80 } else {
81 browser = "opera";
84 return browser;
88 void openUrl (ConString url, bool forceOpera=false) {
89 if (url.length) {
90 import std.stdio : File;
91 import std.process;
92 try {
93 auto frd = File("/dev/null");
94 auto fwr = File("/dev/null", "w");
95 spawnProcess([getBrowserCommand(forceOpera), url.idup], frd, fwr, fwr, null, Config.detached);
96 } catch (Exception e) {
97 conwriteln("ERROR executing URL viewer (", e.msg, ")");
103 // ////////////////////////////////////////////////////////////////////////// //
104 struct UrlInfo {
105 int pos = -1, len = 0;
107 @property bool valid () const pure nothrow @safe @nogc => (pos >= 0 && len > 0);
108 @property int end () const pure nothrow @safe @nogc => (pos >= 0 && len > 0 ? pos+len : 0);
110 static UrlInfo Invalid () pure nothrow @safe @nogc => UrlInfo.init;
114 UrlInfo urlDetect (const(char)[] text) nothrow @trusted @nogc {
115 import iv.strex;
116 UrlInfo res;
117 auto dlpos = text.indexOf("://");
118 if (dlpos < 3) return res;
120 //{ import core.stdc.stdio; printf("det: <%.*s>\n", cast(uint)text.length, text.ptr); }
122 bool isProto (const(char)[] prt) nothrow @trusted @nogc {
123 if (dlpos < prt.length) return false;
124 if (!strEquCI(prt, text[dlpos-prt.length..dlpos])) return false;
125 // check word boundary
126 if (dlpos == prt.length) return true;
127 return !isalpha(text[dlpos-prt.length-1]);
130 if (isProto("ftp")) res.pos = cast(int)(dlpos-3);
131 else if (isProto("http")) res.pos = cast(int)(dlpos-4);
132 else if (isProto("https")) res.pos = cast(int)(dlpos-5);
133 else return res;
135 dlpos += 3; // skip "://"
137 // skip host name
138 for (; dlpos < text.length; ++dlpos) {
139 char ch = text[dlpos];
140 if (ch == '/') break;
141 if (!(isalnum(ch) || ch == '.' || ch == '-' || ch == ':' || ch == '@')) break;
144 // skip path
145 char[64] brcStack;
146 int brcSP = 0;
147 bool wasSharp = false;
149 for (; dlpos < text.length; ++dlpos) {
150 char ch = text[dlpos];
151 // hash
152 if (ch == '#') {
153 if (wasSharp) break;
154 wasSharp = true;
155 brcSP = 0;
156 continue;
158 // opening bracket
159 if (ch == '(' || ch == '[' || ch == '{') {
160 final switch (ch) {
161 case '(': ch = ')'; break;
162 case '[': ch = ']'; break;
163 case '{': ch = '}'; break;
165 if (brcSP < brcStack.length) brcStack[brcSP++] = ch;
166 continue;
168 // closing bracket
169 if (ch == ')' || ch == ']' || ch == '}') {
170 if (brcSP == 0 || brcStack[brcSP-1] != ch) break;
171 --brcSP;
172 continue;
174 if (ch == '.') {
175 if (brcSP == 0) {
176 if (dlpos == text.length || (!isalnum(text[dlpos+1]) && text[dlpos+1] != '_')) break;
178 continue;
180 if (ch <= ' ' || ch >= 127) break;
183 res.len = cast(int)(dlpos-res.pos);
185 return res;
189 // ////////////////////////////////////////////////////////////////////////// //
190 __gshared NVGContext nvg = null;
191 __gshared NVGImage nvgSkullsImg;
192 __gshared NVGImage kittyOut, kittyMsg, kittyFish;
193 __gshared NVGImage[5] statusImgId;
195 __gshared int lastWindowWidth = -1;
198 shared static ~this () {
199 //{ import core.stdc.stdio; printf("******************************\n"); }
200 nvgSkullsImg.clear();
201 //{ import core.stdc.stdio; printf("---\n"); }
202 foreach (ref img; statusImgId[]) img.clear();
203 kittyOut.clear();
204 kittyMsg.clear();
205 kittyFish.clear();
209 void buildStatusImages () {
210 version(none) {
211 statusImgId[ContactStatus.Offline] = nvg.createImageRGBA(16, 16, ctiOffline[], NVGImageFlags.NoFiltering);
212 statusImgId[ContactStatus.Online] = nvg.createImageRGBA(16, 16, ctiOnline[], NVGImageFlags.NoFiltering);
213 statusImgId[ContactStatus.Away] = nvg.createImageRGBA(16, 16, ctiAway[], NVGImageFlags.NoFiltering);
214 statusImgId[ContactStatus.Connecting] = nvg.createImageRGBA(16, 16, ctiOffline[], NVGImageFlags.NoFiltering);
215 } else {
216 //{ import core.stdc.stdio; printf("creating status image: Offline\n"); }
217 statusImgId[ContactStatus.Offline] = nvg.createImageRGBA(16, 16, baph16Gray[], NVGImageFlags.NoFiltering);
218 //{ import core.stdc.stdio; printf("creating status image: Online\n"); }
219 statusImgId[ContactStatus.Online] = nvg.createImageRGBA(16, 16, baph16Online[], NVGImageFlags.NoFiltering);
220 //{ import core.stdc.stdio; printf("creating status image: Away\n"); }
221 statusImgId[ContactStatus.Away] = nvg.createImageRGBA(16, 16, baph16Away[], NVGImageFlags.NoFiltering);
222 //{ import core.stdc.stdio; printf("creating status image: Busy\n"); }
223 statusImgId[ContactStatus.Busy] = nvg.createImageRGBA(16, 16, baph16Busy[], NVGImageFlags.NoFiltering);
224 //{ import core.stdc.stdio; printf("creating status image: Connecting\n"); }
225 statusImgId[ContactStatus.Connecting] = nvg.createImageRGBA(16, 16, baph16Orange[], NVGImageFlags.NoFiltering);
226 //{ import core.stdc.stdio; printf("+++ creatied status images...\n"); }
227 kittyOut = nvg.createImageRGBA(16, 16, kittyOutgoing[], NVGImageFlags.NoFiltering);
228 kittyMsg = nvg.createImageRGBA(16, 16, kittyMessage[], NVGImageFlags.NoFiltering);
229 kittyFish = nvg.createImageRGBA(16, 16, kittyFish16[], NVGImageFlags.NoFiltering);
234 // ////////////////////////////////////////////////////////////////////////// //
235 void loadFonts () {
236 nvg.fonsContext.addFontsFrom(fstash);
237 bndSetFont(nvg.findFont("ui"));
241 // ////////////////////////////////////////////////////////////////////////// //
242 void loadAccount (string nick, bool allowCreate) {
243 if (nick.length == 0) assert(0, "wtf?!");
245 Account acc;
246 bool newAcc = false;
248 try {
249 acc = new Account(nick);
250 } catch (Exception e) {
251 conwriteln("error opening account... (error: ", e.msg, ")");
252 if (!allowCreate) throw e;
253 acc = Account.CreateNew(nick, nick);
254 newAcc = true;
257 // create fake contact
258 if (newAcc && acc.contacts.length == 0 && nick == "_fakeacc") {
259 conwriteln("creating fake contact...");
260 auto c = acc.createEmptyContact();
261 c.info.nick = "test contact";
262 c.info.pubkey[] = 0x55;
263 c.save();
266 clist.buildAccount(acc);
270 // ////////////////////////////////////////////////////////////////////////// //
271 // null: deactivate
272 void doActivateContact (Contact ct) {
273 if (sdmain is null || sdmain.closed) return;
274 if (activeContact is ct) return;
276 activeContact = ct;
277 wipeLog();
278 if (ct is null) {
279 //conwriteln("clear log");
280 if (clist !is null) clist.resetActiveItem();
281 sdmain.title = "BioAcid";
282 glconPostScreenRepaint();
283 } else if (ct.acceptPending) {
284 addTextToLog(ct.acc, ct, LogFile.Msg.Kind.Notification, false, (ct.statusmsg.length ? ct.statusmsg : "I brought you a tasty fish!"), systimeNow);
285 } else {
286 import std.format : format;
287 sdmain.title = "%s [%s] -- BioAcid".format(ct.info.nick, tox_hex(ct.info.pubkey));
288 LogFile log;
289 ct.loadLogInto(log);
290 auto mcount = cast(int)log.messages.length;
291 int left = ct.hmcOnOpen;
292 if (left < ct.unreadCount) left = ct.unreadCount;
293 if (left > mcount) left = mcount;
294 if (mcount > left) mcount = left;
295 if (mcount > 0) {
296 foreach (const ref msg; log.messages[$-mcount..$]) {
297 if (left == ct.unreadCount) addDividerLine();
298 addTextToLog(ct.acc, ct, msg);
299 --left;
302 if (ct.unreadCount != 0) { ct.unreadCount = 0; ct.saveUnreadCount(); }
305 fixTrayIcon();
309 //FIXME: scan all accounts
310 void fixTrayIcon () {
311 if (clist is null) return;
312 auto acc = clist.mainAccount;
313 if (acc is null) return;
314 int unc = 0;
315 foreach (Contact ct; acc) unc += ct.unreadCount;
316 if (unc) {
317 import std.format : format;
318 setTrayUnread();
319 setHint("unread: %d".format(unc));
320 } else {
321 setTrayStatus(acc.status);
322 final switch (acc.status) {
323 case ContactStatus.Connecting: setHint("connecting..."); break;
324 case ContactStatus.Offline: setHint("offline"); break;
325 case ContactStatus.Online: setHint("online"); break;
326 case ContactStatus.Away: setHint("away"); break;
327 case ContactStatus.Busy: setHint("busy"); break;
333 void fixUnreadIndicators () {
334 if (!mainWindowVisible || !mainWindowActive) return; // nothing to do
335 if (activeContact is null || activeContact.unreadCount == 0) return; // nothing to do
336 activeContact.unreadCount = 0;
337 activeContact.unreadCount = 0;
338 activeContact.saveUnreadCount();
339 fixTrayIcon();
343 // ////////////////////////////////////////////////////////////////////////// //
344 void addContactCommands () {
345 conRegFunc!((ConString grpname) {
346 if (clist is null || sdmain is null || sdmain.closed) return;
347 auto acc = clist.mainAccount;
348 if (acc is null) return;
349 if (grpname.length == 0) { conwriteln("group name?"); return; }
350 if (acc.findGroupByName(grpname) != uint.max) { conwriteln("group <", grpname, "> already exists"); return; }
351 auto gid = acc.createGroup(grpname);
352 if (gid == uint.max) { conwriteln("cannot create group <", grpname, ">"); return; }
353 clist.buildAccount(acc);
354 glconPostScreenRepaint();
355 })("group_create", "create group");
358 conRegFunc!((ConString grpname) {
359 if (clist is null || sdmain is null || sdmain.closed) return;
360 if (activeContact is null) { conwriteln("please, select contact first"); return; }
361 if (grpname.length == 0) { conwriteln("group name?"); return; }
362 auto gid = activeContact.acc.createGroup(grpname);
363 if (gid == uint.max) { conwriteln("cannot create group <", grpname, ">"); return; }
364 if (activeContact.acc.moveContactToGroup(activeContact, gid)) {
365 clist.buildAccount(activeContact.acc);
366 glconPostScreenRepaint();
367 } else {
368 conwriteln("cannot move contact to new group");
370 })("move_to_group", "move current contact to named group");
373 conRegFunc!(() {
374 if (clist is null || sdmain is null || sdmain.closed) return;
375 if (activeContact is null) { conwriteln("please, select contact first"); return; }
376 auto acc = activeContact.acc;
377 if (!acc.isOnline) { conwriteln("you must be online to remove contacts"); return; }
378 if (!acc.removeContact(activeContact)) { conwriteln("cannot remove current contact"); return; }
379 wipeLog();
380 activeContact = null;
381 clist.buildAccount(acc);
382 glconPostScreenRepaint();
383 })("contact_remove", "unfriend and remove current contact");