1 /*
2  *Copyright (C) 2018 Laurent Tréguier
3  *
4  *This file is part of DLS.
5  *
6  *DLS is free software: you can redistribute it and/or modify
7  *it under the terms of the GNU General Public License as published by
8  *the Free Software Foundation, either version 3 of the License, or
9  *(at your option) any later version.
10  *
11  *DLS is distributed in the hope that it will be useful,
12  *but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *GNU General Public License for more details.
15  *
16  *You should have received a copy of the GNU General Public License
17  *along with DLS.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 module dls.protocol.messages.window;
22 
23 import dls.protocol.interfaces : MessageActionItem;
24 import dls.util.nullable: Nullable;
25 
26 void showMessageRequest(string id, Nullable!MessageActionItem item)
27 {
28     import dls.protocol.logger : logger;
29     import dls.tools.symbol_tool : SymbolTool;
30     import dls.util.i18n : Tr, tr;
31     import dls.util.uri : Uri;
32     import std.concurrency : locate, receiveOnly, send;
33     import std.process : browse;
34 
35     while (id !in Util.messageRequestInfo)
36     {
37         auto data = receiveOnly!(Util.ThreadMessageData)();
38         Util.bindMessageToRequestId(data[0], data[1], data[2]);
39     }
40 
41     if (!item.isNull)
42     {
43         switch (Util.messageRequestInfo[id][0])
44         {
45         case Tr.app_upgradeSelections:
46             if (item.title == tr(Tr.app_upgradeSelections_upgrade))
47             {
48                 auto uri = new Uri(Util.messageRequestInfo[id][1]);
49                 SymbolTool.instance.upgradeSelections(uri);
50             }
51 
52             break;
53 
54         case Tr.app_upgradeDls:
55             send(locate(Util.messageRequestInfo[id][1]),
56                     item.title == tr(Tr.app_upgradeDls_upgrade));
57             break;
58 
59         case Tr.app_showChangelog:
60             if (item.title == tr(Tr.app_showChangelog_show))
61             {
62                 logger.info("Opening changelog in browser");
63                 browse(Util.messageRequestInfo[id][1]);
64             }
65 
66             break;
67 
68         default:
69             assert(false, Util.messageRequestInfo[id][0] ~ " cannot be handled as requests");
70         }
71     }
72 
73     Util.messageRequestInfo.remove(id);
74 }
75 
76 final abstract class Util
77 {
78     import dls.protocol.jsonrpc : send;
79     import dls.protocol.messages.methods : Window;
80     import dls.util.i18n : Tr, tr, trType;
81     import std.array : array;
82     import std.algorithm : map;
83     import std.typecons : Tuple, tuple;
84 
85     shared alias ThreadMessageData = Tuple!(string, Tr, string);
86 
87     private static Tuple!(Tr, string)[string] messageRequestInfo;
88 
89     static void sendMessage(Tr message, string[] args = [])
90     {
91         import dls.protocol.interfaces : ShowMessageParams;
92 
93         send(Window.showMessage, new ShowMessageParams(trType(message), tr(message, args)));
94     }
95 
96     static string sendMessageRequest(Tr message, Tr[] actions, string[] args = [])
97     {
98         import dls.protocol.interfaces : ShowMessageRequestParams;
99         import dls.util.nullable: nullable;
100 
101         return send(Window.showMessageRequest, new ShowMessageRequestParams(trType(message),
102                 tr(message, args), actions.map!(a => new MessageActionItem(tr(a,
103                 args))).array.nullable));
104     }
105 
106     static void bindMessageToRequestId(string id, Tr message, string data = null)
107     {
108         messageRequestInfo[id] = tuple(message, data);
109     }
110 }