save toxcore data with proper access rights
[bioacid.git] / tklog.d
blob03c6c451313c95bd229562d20d5fdf9f9986c706
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 MessageOutMark : LayObject {
83 long msgid; // >=0: outgoing, unacked yet (0: sent in offline mode)
84 TextDigest digest; // text digest
85 SysTime time;
87 this (long aid, SysTime atime, const(void)[] atext) nothrow @safe @nogc { msgid = aid; time = atime; digest = textDigest(atext); }
89 override int width () => kittyOut.width;
90 override int spacewidth () => 4;
91 override int height () => kittyOut.height;
92 override int ascent () => 0;
93 override int descent () => 0;
94 override bool canbreak () => true;
95 override bool spaced () => false;
96 // y is at baseline
97 override void draw (NVGContext ctx, float x, float y) {
98 if (msgid >= 0) {
99 nvg.save();
100 scope(exit) nvg.restore;
101 nvg.newPath();
102 // +3 is a hack
103 nvg.rect(x, y-height+3, width, height);
104 nvg.fillPaint(nvg.imagePattern(x, y-height+3, width, height, 0, kittyOut));
105 nvg.fill();
109 final bool isOurMark (const(void)[] atext, SysTime atime) nothrow @trusted {
110 pragma(inline, true);
111 return (atime == time && digest[] == textDigest(atext));
114 final bool isOurMark (long aid, const(void)[] atext, SysTime atime) nothrow @trusted {
115 pragma(inline, true);
116 return (aid > 0 ? (msgid == aid) : (atime == time && digest[] == textDigest(atext)));
121 // ////////////////////////////////////////////////////////////////////////// //
122 void logFixAckMessageId (long oldid, long newid, const(void)[] text, SysTime time) {
123 if (oldid == newid) return; // nothing to do
124 foreach (immutable uint widx; 0..lay.wordCount) {
125 auto w = lay.wordByIndex(widx);
126 int oidx = w.objectIdx;
127 if (oidx >= 0) {
128 if (auto maw = cast(MessageOutMark)lay.objectAtIndex(oidx)) {
129 if (maw.isOurMark(oldid, text, time)) {
130 maw.msgid = newid;
131 glconPostScreenRepaint(); // redraw
139 void logResetAckMessageIds () {
140 foreach (immutable uint widx; 0..lay.wordCount) {
141 auto w = lay.wordByIndex(widx);
142 int oidx = w.objectIdx;
143 if (oidx >= 0) {
144 if (auto maw = cast(MessageOutMark)lay.objectAtIndex(oidx)) {
145 maw.msgid = 0;
146 glconPostScreenRepaint(); // redraw
153 void ackLogMessage (long msgid) {
154 if (msgid <= 0) return;
155 foreach (immutable uint widx; 0..lay.wordCount) {
156 auto w = lay.wordByIndex(widx);
157 int oidx = w.objectIdx;
158 if (oidx >= 0) {
159 if (auto maw = cast(MessageOutMark)lay.objectAtIndex(oidx)) {
160 if (maw.msgid == msgid) {
161 maw.msgid = -1; // reset mark
162 glconPostScreenRepaint(); // redraw
170 // coordinates are adjusted so (0, 0) points to logical layouter top-left
172 bool logCheckCancelMark (int mx, int my) {
173 bool removeFromResendQueue (long msgid, in ref TextDigest digest, SysTime time) {
177 // ////////////////////////////////////////////////////////////////////////// //
178 void wipeLog () {
179 lay.wipeAll(true); // clear log, but delete objects
180 layUrlList.clear();
181 layOffset = 0;
185 void addDividerLine (bool doflushgui=false) {
186 if (glconCtlWindow is null || glconCtlWindow.closed) return;
189 bool inFrame = nvg.inFrame;
190 if (!inFrame) {
191 glconCtlWindow.setAsCurrentOpenGlContext(); // make this window active
192 nvg.beginFrame(glconCtlWindow.width, glconCtlWindow.height);
194 scope(exit) {
195 if (!inFrame) {
196 nvg.endFrame();
197 if (doflushgui) flushGui();
198 glconCtlWindow.releaseCurrentOpenGlContext();
202 lay.fontStyle.fontsize = 2;
203 lay.fontStyle.color = NVGColor.k8orange.asUint;
204 lay.fontStyle.bgcolor = NVGColor("#aa0").asUint;
205 lay.fontStyle.monospace = true;
207 lay.putExpander();
208 lay.endPara();
209 lay.finalize();
211 // redraw
212 glconPostScreenRepaint();
216 // `ct` can be `null` for "my message" or "system message"
217 void addTextToLog (Account acc, Contact ct, LogFile.Msg.Kind kind, bool action, const(char)[] msg, SysTime time, long msgid=-1, bool doflushgui=false) {
218 if (glconCtlWindow is null || glconCtlWindow.closed) return;
221 bool inFrame = nvg.inFrame;
222 if (!inFrame) {
223 glconCtlWindow.setAsCurrentOpenGlContext(); // make this window active
224 nvg.beginFrame(glconCtlWindow.width, glconCtlWindow.height);
226 scope(exit) {
227 if (!inFrame) {
228 nvg.endFrame();
229 if (doflushgui) flushGui();
230 glconCtlWindow.releaseCurrentOpenGlContext();
234 // add "message start" mark
235 lay.putObject(new MessageStart(msgid));
237 lay.fontStyle.fontsize = 16;
238 lay.fontStyle.color = NVGColor.k8orange.asUint;
239 lay.fontStyle.bgcolor = NVGColor("#222").asUint;
240 lay.fontStyle.monospace = true;
242 NVGColor textColor;
244 final switch (kind) {
245 case LogFile.Msg.Kind.Outgoing: textColor = NVGColor.k8orange; lay.fontStyle.color = NVGColor("#c40").asUint; lay.put(acc.info.nick); break;
246 case LogFile.Msg.Kind.Incoming: textColor = NVGColor("#ccc"); lay.fontStyle.color = NVGColor("#666").asUint; lay.put(ct.info.nick); break;
247 case LogFile.Msg.Kind.Notification: textColor = NVGColor("#0c0"); lay.fontStyle.color = textColor.asUint; lay.put("*system*"); break;
250 lay.fontStyle.monospace = false;
251 //lay.putHardSpace(64);
252 lay.putExpander();
253 // add "message outgoing" mark
254 if (kind == LogFile.Msg.Kind.Outgoing) {
255 //conwriteln("OG: msgid=", msgid);
256 if (msgid <= 0) {
257 msgid = ct.findInResendQueue(msg, time);
258 //conwriteln(" new msgid=", msgid);
260 lay.putObject(new MessageOutMark(msgid, time, msg));
261 lay.putExpander();
264 if (kind == LogFile.Msg.Kind.Incoming) lay.fontStyle.color = NVGColor("#888").asUint;
266 import std.datetime;
267 import std.format : format;
268 auto dt = cast(DateTime)time;
269 string tstr = "%04u/%02u/%02u".format(dt.year, dt.month, dt.day);
270 lay.put(tstr);
271 lay.putNBSP();
274 lay.fontStyle.color = (kind == LogFile.Msg.Kind.Incoming ? NVGColor("#888").asUint : NVGColor("#666").asUint);
275 lay.fontStyle.bgcolor = NVGColor("#006").asUint;
277 import std.datetime;
278 import std.format : format;
279 auto dt = cast(DateTime)time;
280 string tstr = "%02u:%02u:%02u".format(dt.hour, dt.minute, dt.second);
281 lay.put(tstr);
282 lay.putNBSP();
284 lay.endPara();
286 lay.fontStyle.bgcolor = NVGColor.transparent.asUint;
287 lay.fontStyle.fontsize = 20;
289 if (action) {
290 lay.fontStyle.color = NVGColor("#fff").asUint;
291 lay.put("/me ");
294 void xput (const(char)[] str) {
295 while (str.length) {
296 auto nl = str.indexOf('\n');
297 if (nl < 0) { lay.put(str); break; }
298 if (nl > 0) lay.put(str[0..nl]);
299 lay.endPara();
300 str = str[nl+1..$];
304 msg = msg.xstripright;
306 lay.fontStyle.color = textColor.asUint;
307 while (msg.length) {
308 auto nfo = urlDetect(msg);
309 if (!nfo.valid) {
310 xput(msg);
311 break;
313 // url found
314 xput(msg[0..nfo.pos]);
315 string url = msg[nfo.pos..nfo.end].idup;
316 msg = msg[nfo.end..$];
317 auto stword = lay.nextWordIndex;
318 lay.pushStyles();
319 auto c = lay.fontStyle.color;
320 scope(exit) { lay.popStyles; lay.fontStyle.color = c; }
321 lay.fontStyle.href = true;
322 lay.fontStyle.underline = true;
323 lay.fontStyle.color = NVGColor("#06f").asUint;
324 lay.put(url);
325 while (stword < lay.nextWordIndex) {
326 layUrlList[stword] = LayUrl(url, stword);
327 ++stword;
331 lay.endPara();
332 lay.finalize();
334 // redraw
335 glconPostScreenRepaint();
339 void addTextToLog (Account acc, Contact ct, in ref LogFile.Msg msg, long msgid=-1, bool doflushgui=false) {
340 import iv.utfutil : utf8Encode;
341 char[] text;
342 text.reserve(4096);
343 scope(exit) delete text;
344 // decode text
345 foreach (dchar dc; msg.byDChar) {
346 char[4] buf = void;
347 auto len = utf8Encode(buf[], dc);
348 text ~= buf[0..len];
350 addTextToLog(acc, ct, msg.kind, msg.isMe, text, msg.time, msgid, doflushgui);