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 std.typecons : Nullable; 25 26 void showMessageRequest(string id, Nullable!MessageActionItem item) 27 { 28 import dls.tools.symbol_tool : SymbolTool; 29 import dls.util.constants : Tr; 30 import dls.util.i18n : tr; 31 import dls.util.logger : logger; 32 import dls.util.uri : Uri; 33 import std.concurrency : locate, receiveOnly, send; 34 import std.path : dirName; 35 import std.process : browse; 36 37 while (id !in Util.messageRequestInfo) 38 { 39 auto data = receiveOnly!(Util.ThreadMessageData)(); 40 Util.bindMessageToRequestId(data[0], data[1], data[2]); 41 } 42 43 if (!item.isNull) 44 { 45 switch (Util.messageRequestInfo[id][0]) 46 { 47 case Tr.app_upgradeSelections: 48 if (item.title == tr(Tr.app_upgradeSelections_upgrade)) 49 { 50 auto uri = new Uri(Util.messageRequestInfo[id][1]); 51 SymbolTool.instance.upgradeSelections(uri); 52 } 53 54 break; 55 56 case Tr.app_upgradeDls: 57 send(locate(Util.messageRequestInfo[id][1]), 58 item.title == tr(Tr.app_upgradeDls_upgrade)); 59 break; 60 61 case Tr.app_showChangelog: 62 if (item.title == tr(Tr.app_showChangelog_show)) 63 { 64 logger.info("Opening changelog in browser"); 65 browse(Util.messageRequestInfo[id][1]); 66 } 67 68 break; 69 70 default: 71 assert(false, Util.messageRequestInfo[id][0] ~ " cannot be handled as requests"); 72 } 73 } 74 75 Util.messageRequestInfo.remove(id); 76 } 77 78 final abstract class Util 79 { 80 import dls.protocol.interfaces : MessageType; 81 import dls.protocol.jsonrpc : send; 82 import dls.protocol.messages.methods : Window; 83 import dls.util.constants : Tr; 84 import dls.util.i18n : tr, trType; 85 import std.array : array, replace; 86 import std.algorithm : map; 87 import std.conv : to; 88 import std.json : JSONValue, parseJSON; 89 import std.typecons : Tuple, tuple; 90 91 shared alias ThreadMessageData = Tuple!(string, Tr, string); 92 93 private static Tuple!(Tr, string)[string] messageRequestInfo; 94 95 static void sendMessage(Tr message, string[] args = []) 96 { 97 import dls.protocol.interfaces : ShowMessageParams; 98 99 send(Window.showMessage, new ShowMessageParams(trType(message), tr(message, args))); 100 } 101 102 static string sendMessageRequest(Tr message, Tr[] actions, string[] args = []) 103 { 104 import dls.protocol.interfaces : ShowMessageRequestParams; 105 import std.typecons : nullable; 106 107 return send(Window.showMessageRequest, new ShowMessageRequestParams(trType(message), 108 tr(message, args), actions.map!(a => new MessageActionItem(tr(a, 109 args))).array.nullable)); 110 } 111 112 static void bindMessageToRequestId(string id, Tr message, string data = null) 113 { 114 messageRequestInfo[id] = tuple(message, data); 115 } 116 }