i just can't stop...
[bioacid.git] / tklog.d
blob834a89e11109eb19ccadc489b59b93b1205a4ade
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 tklog 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.nanovega;
28 import iv.nanovega.blendish;
29 import iv.nanovega.textlayouter;
30 import iv.strex;
31 import iv.tox;
32 import iv.unarray;
33 import iv.utfutil;
34 import iv.vfs.io;
36 import accdb;
37 import accobj;
38 import fonts;
39 import popups;
40 import icondata;
41 import notifyicon;
42 import toxproto;
44 import tkmain;
47 // ////////////////////////////////////////////////////////////////////////// //
48 alias LayTextClass = LayTextD;
51 // ////////////////////////////////////////////////////////////////////////// //
52 __gshared LayTextClass lay;
53 __gshared int layWinHeight = 0;
54 __gshared int layOffset = 0; // from bottom
56 static struct LayUrl {
57 string url;
58 uint wordidx; // first word
61 __gshared LayUrl[uint] layUrlList; // for each word
64 // ////////////////////////////////////////////////////////////////////////// //
65 class MessageStart : LayObject {
66 long msgid; // >0: outgoing, unacked yet
68 this (long aid=-1) nothrow @safe @nogc { msgid = aid; }
70 override int width () => 0;
71 override int spacewidth () => 0;
72 override int height () => 0;
73 override int ascent () => 0;
74 override int descent () => 0;
75 override bool canbreak () => true;
76 override bool spaced () => false;
77 // y is at baseline
78 override void draw (NVGContext ctx, float x, float y) {}
82 class MessageDividerStart : LayObject {
83 long msgid; // >0: outgoing, unacked yet
85 this (long aid=-1) nothrow @safe @nogc { msgid = aid; }
87 override int width () => 0;
88 override int spacewidth () => 0;
89 override int height () => 0;
90 override int ascent () => 0;
91 override int descent () => 0;
92 override bool canbreak () => true;
93 override bool spaced () => false;
94 // y is at baseline
95 override void draw (NVGContext ctx, float x, float y) {}
99 class MessageOutMark : LayObject {
100 long msgid; // >=0: outgoing, unacked yet (0: sent in offline mode)
101 TextDigest digest; // text digest
102 SysTime time;
104 this (long aid, SysTime atime, const(void)[] atext) nothrow @safe @nogc { msgid = aid; time = atime; digest = textDigest(atext); }
106 override int width () => kittyOut.width;
107 override int spacewidth () => 4;
108 override int height () => kittyOut.height;
109 override int ascent () => 0;
110 override int descent () => 0;
111 override bool canbreak () => true;
112 override bool spaced () => false;
113 // y is at baseline
114 override void draw (NVGContext ctx, float x, float y) {
115 if (msgid >= 0) {
116 nvg.save();
117 scope(exit) nvg.restore;
118 nvg.newPath();
119 // +3 is a hack
120 nvg.rect(x, y-height+3, width, height);
121 nvg.fillPaint(nvg.imagePattern(x, y-height+3, width, height, 0, kittyOut));
122 nvg.fill();
126 final bool isOurMark (const(void)[] atext, SysTime atime) nothrow @trusted {
127 pragma(inline, true);
128 return (atime == time && digest[] == textDigest(atext));
131 final bool isOurMark (long aid, const(void)[] atext, SysTime atime) nothrow @trusted {
132 pragma(inline, true);
133 return (aid > 0 ? (msgid == aid) : (atime == time && digest[] == textDigest(atext)));
138 // ////////////////////////////////////////////////////////////////////////// //
139 void logFixAckMessageId (long oldid, long newid, const(void)[] text, SysTime time) {
140 if (oldid == newid) return; // nothing to do
141 foreach (immutable uint widx; 0..lay.wordCount) {
142 auto w = lay.wordByIndex(widx);
143 int oidx = w.objectIdx;
144 if (oidx >= 0) {
145 if (auto maw = cast(MessageOutMark)lay.objectAtIndex(oidx)) {
146 if (maw.isOurMark(oldid, text, time)) {
147 maw.msgid = newid;
148 glconPostScreenRepaint(); // redraw
155 void logResetAckMessageIds () {
156 foreach (immutable uint widx; 0..lay.wordCount) {
157 auto w = lay.wordByIndex(widx);
158 int oidx = w.objectIdx;
159 if (oidx >= 0) {
160 if (auto maw = cast(MessageOutMark)lay.objectAtIndex(oidx)) {
161 maw.msgid = 0;
162 glconPostScreenRepaint(); // redraw
168 void ackLogMessage (long msgid) {
169 if (msgid <= 0) return;
170 foreach (immutable uint widx; 0..lay.wordCount) {
171 auto w = lay.wordByIndex(widx);
172 int oidx = w.objectIdx;
173 if (oidx >= 0) {
174 if (auto maw = cast(MessageOutMark)lay.objectAtIndex(oidx)) {
175 if (maw.msgid == msgid) {
176 maw.msgid = -1; // reset mark
177 glconPostScreenRepaint(); // redraw
185 // coordinates are adjusted so (0, 0) points to logical layouter top-left
187 bool logCheckCancelMark (int mx, int my) {
188 bool removeFromResendQueue (long msgid, in ref TextDigest digest, SysTime time) {
192 // ////////////////////////////////////////////////////////////////////////// //
193 void wipeLog () {
194 lay.wipeAll(true); // clear log, but delete objects
195 layUrlList.clear();
196 layOffset = 0;
200 // ////////////////////////////////////////////////////////////////////////// //
201 void addDividerLine (bool doflushgui=false) {
202 if (glconCtlWindow is null || glconCtlWindow.closed) return;
205 bool inFrame = nvg.inFrame;
206 if (!inFrame) {
207 glconCtlWindow.setAsCurrentOpenGlContext(); // make this window active
208 nvg.beginFrame(glconCtlWindow.width, glconCtlWindow.height);
210 scope(exit) {
211 if (!inFrame) {
212 nvg.endFrame();
213 if (doflushgui) flushGui();
214 glconCtlWindow.releaseCurrentOpenGlContext();
218 uint widx = 0;
219 while (widx < lay.wordCount) {
220 auto w = lay.wordByIndex(widx);
221 int oidx = w.objectIdx;
222 if (oidx >= 0) {
223 if (auto maw = cast(MessageDividerStart)lay.objectAtIndex(oidx)) {
224 // this always followed by expander; remove them both
225 lay.removeWordsAt(widx, 2);
226 continue;
229 ++widx;
232 lay.fontStyle.fontsize = 2;
233 lay.fontStyle.color = NVGColor.k8orange.asUint;
234 lay.fontStyle.bgcolor = NVGColor("#aa0").asUint;
235 lay.fontStyle.monospace = true;
237 lay.putObject(new MessageDividerStart());
238 lay.putExpander();
239 lay.endPara();
240 lay.finalize();
243 // redraw
244 glconPostScreenRepaint();
248 // ////////////////////////////////////////////////////////////////////////// //
249 // `ct` can be `null` for "my message" or "system message"
250 void addTextToLog (Account acc, Contact ct, LogFile.Msg.Kind kind, bool action, const(char)[] msg, SysTime time, long msgid=-1, bool doflushgui=false) {
251 if (glconCtlWindow is null || glconCtlWindow.closed) return;
254 bool inFrame = nvg.inFrame;
255 if (!inFrame) {
256 glconCtlWindow.setAsCurrentOpenGlContext(); // make this window active
257 nvg.beginFrame(glconCtlWindow.width, glconCtlWindow.height);
259 scope(exit) {
260 if (!inFrame) {
261 nvg.endFrame();
262 if (doflushgui) flushGui();
263 glconCtlWindow.releaseCurrentOpenGlContext();
267 // add "message start" mark
268 lay.putObject(new MessageStart(msgid));
270 lay.fontStyle.fontsize = 16;
271 lay.fontStyle.color = NVGColor.k8orange.asUint;
272 lay.fontStyle.bgcolor = NVGColor("#222").asUint;
273 lay.fontStyle.monospace = true;
275 NVGColor textColor;
277 final switch (kind) {
278 case LogFile.Msg.Kind.Outgoing: textColor = NVGColor.k8orange; lay.fontStyle.color = NVGColor("#c40").asUint; lay.put(acc.info.nick); break;
279 case LogFile.Msg.Kind.Incoming: textColor = NVGColor("#ccc"); lay.fontStyle.color = NVGColor("#666").asUint; lay.put(ct.info.nick); break;
280 case LogFile.Msg.Kind.Notification: textColor = NVGColor("#0c0"); lay.fontStyle.color = textColor.asUint; lay.put("*system*"); break;
283 lay.fontStyle.monospace = false;
284 //lay.putHardSpace(64);
285 lay.putExpander();
286 // add "message outgoing" mark
287 if (kind == LogFile.Msg.Kind.Outgoing) {
288 //conwriteln("OG: msgid=", msgid);
289 if (msgid <= 0) {
290 msgid = ct.findInResendQueue(msg, time);
291 //conwriteln(" new msgid=", msgid);
293 lay.putObject(new MessageOutMark(msgid, time, msg));
294 lay.putExpander();
297 if (kind == LogFile.Msg.Kind.Incoming) lay.fontStyle.color = NVGColor("#888").asUint;
299 import std.datetime;
300 import std.format : format;
301 auto dt = cast(DateTime)time;
302 string tstr = "%04u/%02u/%02u".format(dt.year, dt.month, dt.day);
303 lay.put(tstr);
304 lay.putNBSP();
307 lay.fontStyle.color = (kind == LogFile.Msg.Kind.Incoming ? NVGColor("#888").asUint : NVGColor("#666").asUint);
308 lay.fontStyle.bgcolor = NVGColor("#006").asUint;
310 import std.datetime;
311 import std.format : format;
312 auto dt = cast(DateTime)time;
313 string tstr = "%02u:%02u:%02u".format(dt.hour, dt.minute, dt.second);
314 lay.put(tstr);
315 lay.putNBSP();
317 lay.endPara();
319 lay.fontStyle.bgcolor = NVGColor.transparent.asUint;
320 lay.fontStyle.fontsize = 20;
322 if (action) {
323 lay.fontStyle.color = NVGColor("#fff").asUint;
324 lay.put("/me ");
327 void xput (const(char)[] str) {
328 while (str.length) {
329 auto nl = str.indexOf('\n');
330 if (nl < 0) { lay.put(str); break; }
331 if (nl > 0) lay.put(str[0..nl]);
332 lay.endPara();
333 str = str[nl+1..$];
337 msg = msg.xstripright;
339 lay.fontStyle.color = textColor.asUint;
340 while (msg.length) {
341 auto nfo = urlDetect(msg);
342 if (!nfo.valid) {
343 xput(msg);
344 break;
346 // url found
347 xput(msg[0..nfo.pos]);
348 string url = msg[nfo.pos..nfo.end].idup;
349 msg = msg[nfo.end..$];
350 auto stword = lay.nextWordIndex;
351 lay.pushStyles();
352 auto c = lay.fontStyle.color;
353 scope(exit) { lay.popStyles; lay.fontStyle.color = c; }
354 lay.fontStyle.href = true;
355 lay.fontStyle.underline = true;
356 lay.fontStyle.color = NVGColor("#06f").asUint;
357 lay.put(url);
358 while (stword < lay.nextWordIndex) {
359 layUrlList[stword] = LayUrl(url, stword);
360 ++stword;
364 lay.endPara();
365 lay.finalize();
367 // redraw
368 glconPostScreenRepaint();
372 void addTextToLog (Account acc, Contact ct, in ref LogFile.Msg msg, long msgid=-1, bool doflushgui=false) {
373 import iv.utfutil : utf8Encode;
374 char[] text;
375 text.reserve(4096);
376 scope(exit) delete text;
377 // decode text
378 foreach (dchar dc; msg.byDChar) {
379 char[4] buf = void;
380 auto len = utf8Encode(buf[], dc);
381 text ~= buf[0..len];
383 addTextToLog(acc, ct, msg.kind, msg.isMe, text, msg.time, msgid, doflushgui);