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