| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2020 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include "layer.h" |
| 14 | #include "writer.h" |
| 15 | #include "config.h" |
| 16 | |
| 17 | using namespace smooth::GUI::Dialogs; |
| 18 | |
| 19 | BoCA::LayerProtocols::LayerProtocols() : Layer("Logs") |
| 20 | { |
| 21 | text_protocol = new Text(NIL, Point(7, 10)); |
| 22 | |
| 23 | combo_protocol = new ComboBox(Point(396, 7), Size(300, 0)); |
| 24 | combo_protocol->onSelectEntry.Connect(&LayerProtocols::SelectProtocol, this); |
| 25 | combo_protocol->SetOrientation(OR_UPPERRIGHT); |
| 26 | |
| 27 | btn_save = new Button(String(), Point(88, 6), Size()); |
| 28 | btn_save->onAction.Connect(&LayerProtocols::SaveProtocol, this); |
| 29 | btn_save->SetOrientation(OR_UPPERRIGHT); |
| 30 | |
| 31 | list_protocol = new ListBox(Point(7, 34), Size(500, 360)); |
| 32 | |
| 33 | Add(text_protocol); |
| 34 | Add(combo_protocol); |
| 35 | Add(btn_save); |
| 36 | |
| 37 | Add(list_protocol); |
| 38 | |
| 39 | Deactivate(); |
| 40 | |
| 41 | UpdateProtocolList(); |
| 42 | |
| 43 | /* Update language settings in case the layer is instantiated late. |
| 44 | */ |
| 45 | OnChangeLanguageSettings(); |
| 46 | |
| 47 | /* Connect slots. |
| 48 | */ |
| 49 | onChangeSize.Connect(&LayerProtocols::OnChangeSize, this); |
| 50 | |
| 51 | Settings::Get()->onChangeLanguageSettings.Connect(&LayerProtocols::OnChangeLanguageSettings, this); |
| 52 | |
| 53 | Protocol::onUpdateProtocolList.Connect(&LayerProtocols::UpdateProtocolList, this); |
| 54 | Protocol::onUpdateProtocol.Connect(&LayerProtocols::UpdateProtocol, this); |
| 55 | } |
| 56 | |
| 57 | BoCA::LayerProtocols::~LayerProtocols() |
| 58 | { |
| 59 | /* Disconnect slots. |
| 60 | */ |
| 61 | Settings::Get()->onChangeLanguageSettings.Disconnect(&LayerProtocols::OnChangeLanguageSettings, this); |
| 62 | |
| 63 | Protocol::onUpdateProtocolList.Disconnect(&LayerProtocols::UpdateProtocolList, this); |
| 64 | Protocol::onUpdateProtocol.Disconnect(&LayerProtocols::UpdateProtocol, this); |
| 65 | |
| 66 | /* Delete widgets. |
| 67 | */ |
| 68 | DeleteObject(text_protocol); |
| 69 | DeleteObject(combo_protocol); |
| 70 | DeleteObject(btn_save); |
| 71 | |
| 72 | DeleteObject(list_protocol); |
| 73 | } |
| 74 | |
| 75 | /* Called when component canvas size changes. |
| 76 | * ---- |
| 77 | */ |
| 78 | Void BoCA::LayerProtocols::OnChangeSize(const Size &nSize) |
| 79 | { |
| 80 | Rect clientRect = Rect(GetPosition(), GetSize()); |
| 81 | Size clientSize = Size(clientRect.GetWidth(), clientRect.GetHeight()); |
| 82 | |
| 83 | list_protocol->SetSize(clientSize - Size(15, 42)); |
| 84 | } |
| 85 | |
| 86 | /* Called when application language is changed. |
| 87 | * ---- |
| 88 | */ |
| 89 | Void BoCA::LayerProtocols::OnChangeLanguageSettings() |
| 90 | { |
| 91 | I18n *i18n = I18n::Get(); |
| 92 | |
| 93 | i18n->SetContext("Extensions::Logging"); |
| 94 | |
| 95 | SetText(i18n->TranslateString("Logs")); |
| 96 | |
| 97 | /* Hide all affected widgets prior to changing |
| 98 | * labels to avoid flickering. |
| 99 | */ |
| 100 | Bool prevVisible = IsVisible(); |
| 101 | |
| 102 | if (prevVisible) Hide(); |
| 103 | |
| 104 | /* Set texts and positions. |
| 105 | */ |
| 106 | text_protocol->SetText(i18n->AddColon(i18n->TranslateString("Log"))); |
| 107 | |
| 108 | btn_save->SetText(i18n->TranslateString("Save as")); |
| 109 | btn_save->SetWidth(Math::Max(btn_save->GetUnscaledTextWidth() + 14, 80)); |
| 110 | btn_save->SetX(btn_save->GetWidth() + 8); |
| 111 | |
| 112 | combo_protocol->SetX(btn_save->GetWidth() + 316); |
| 113 | |
| 114 | list_protocol->RemoveAllTabs(); |
| 115 | |
| 116 | list_protocol->AddTab(i18n->TranslateString("Time"), list_protocol->GetFont().GetUnscaledTextSizeX("00:00:00.000") + 4, i18n->IsActiveLanguageRightToLeft() ? OR_LEFT : OR_RIGHT); |
| 117 | list_protocol->AddTab(i18n->TranslateString("Message")); |
| 118 | |
| 119 | /* OnChangeSize will correct sizes of any other widgets. |
| 120 | */ |
| 121 | OnChangeSize(GetSize()); |
| 122 | |
| 123 | /* Show all widgets again. |
| 124 | */ |
| 125 | if (prevVisible) Show(); |
| 126 | } |
| 127 | |
| 128 | Void BoCA::LayerProtocols::UpdateProtocolList() |
| 129 | { |
| 130 | Threads::Lock lock(mutex); |
| 131 | |
| 132 | /* Save selected protocol name. |
| 133 | */ |
| 134 | String selectedName; |
| 135 | |
| 136 | if (combo_protocol->GetSelectedEntry() != NIL) selectedName = combo_protocol->GetSelectedEntry()->GetText(); |
| 137 | |
| 138 | /* Update protocol combo list. |
| 139 | */ |
| 140 | Surface *surface = GetDrawSurface(); |
| 141 | Bool visible = IsVisible(); |
| 142 | |
| 143 | if (visible) surface->StartPaint(Rect(combo_protocol->GetRealPosition(), combo_protocol->GetRealSize())); |
| 144 | |
| 145 | combo_protocol->RemoveAllEntries(); |
| 146 | |
| 147 | const Array<Protocol *> &protocols = Protocol::Get(); |
| 148 | |
| 149 | foreachreverse (Protocol *protocol, protocols) |
| 150 | { |
| 151 | #ifndef DEBUG |
| 152 | if (protocol->GetName() == "Debug output") continue; |
| 153 | #endif |
| 154 | /* Get protocol display name. |
| 155 | */ |
| 156 | Int index = Int64(protocol) & 0xFFFFFFFF; |
| 157 | String displayName = displayNames.Get(index); |
| 158 | |
| 159 | if (displayName == NIL) |
| 160 | { |
| 161 | DateTime currentTime = DateTime::Current(); |
| 162 | |
| 163 | displayName = String(currentTime.GetHour() < 10 ? "0" : NIL).Append(String::FromInt(currentTime.GetHour())).Append(":") |
| 164 | .Append(String(currentTime.GetMinute() < 10 ? "0" : NIL).Append(String::FromInt(currentTime.GetMinute()))).Append(":") |
| 165 | .Append(String(currentTime.GetSecond() < 10 ? "0" : NIL).Append(String::FromInt(currentTime.GetSecond()))).Append(" - ").Append(protocol->GetName()); |
| 166 | |
| 167 | displayNames.Add(displayName, index); |
| 168 | } |
| 169 | |
| 170 | /* Add protocol to list. |
| 171 | */ |
| 172 | ListEntry *entry = combo_protocol->AddEntry(displayName); |
| 173 | |
| 174 | if (displayName == selectedName) |
| 175 | { |
| 176 | combo_protocol->SelectEntry(entry); |
| 177 | combo_protocol->Paint(SP_PAINT); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (visible) surface->EndPaint(); |
| 182 | |
| 183 | if (combo_protocol->Length() > 0) Activate(); |
| 184 | } |
| 185 | |
| 186 | Void BoCA::LayerProtocols::UpdateProtocol(const String &name) |
| 187 | { |
| 188 | Threads::Lock lock(mutex); |
| 189 | |
| 190 | if (combo_protocol->GetSelectedEntry() == NIL) return; |
| 191 | |
| 192 | String displayName = combo_protocol->GetSelectedEntry()->GetText(); |
| 193 | String protocolName = displayName.Tail(displayName.Length() - 11); |
| 194 | |
| 195 | Protocol *protocol = Protocol::Get(protocolName); |
| 196 | |
| 197 | if (name == protocol->GetName()) |
| 198 | { |
| 199 | Surface *surface = GetDrawSurface(); |
| 200 | Bool visible = IsVisible(); |
| 201 | |
| 202 | if (visible) surface->StartPaint(Rect(list_protocol->GetRealPosition(), list_protocol->GetRealSize())); |
| 203 | |
| 204 | const Array<String> &messages = protocol->GetMessages(); |
| 205 | |
| 206 | for (Int i = list_protocol->Length(); i < messages.Length(); i++) |
| 207 | { |
| 208 | const String &message = messages.GetNth(i); |
| 209 | |
| 210 | list_protocol->AddEntry(message.SubString(0, 12).Append(ListEntry::tabDelimiter).Append(message.Tail(message.Length() - 15))); |
| 211 | } |
| 212 | |
| 213 | if (visible) surface->EndPaint(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | Void BoCA::LayerProtocols::SelectProtocol() |
| 218 | { |
| 219 | Threads::Lock lock(mutex); |
| 220 | |
| 221 | if (combo_protocol->GetSelectedEntry() == NIL) return; |
| 222 | |
| 223 | Surface *surface = GetDrawSurface(); |
| 224 | Bool visible = IsVisible(); |
| 225 | |
| 226 | if (visible) surface->StartPaint(Rect(list_protocol->GetRealPosition(), list_protocol->GetRealSize())); |
| 227 | |
| 228 | String displayName = combo_protocol->GetSelectedEntry()->GetText(); |
| 229 | String protocolName = displayName.Tail(displayName.Length() - 11); |
| 230 | |
| 231 | const Array<String> &messages = Protocol::Get(protocolName)->GetMessages(); |
| 232 | |
| 233 | list_protocol->Hide(); |
| 234 | list_protocol->RemoveAllEntries(); |
| 235 | |
| 236 | foreach (const String &message, messages) |
| 237 | { |
| 238 | list_protocol->AddEntry(message.SubString(0, 12).Append(ListEntry::tabDelimiter).Append(message.Tail(message.Length() - 15))); |
| 239 | } |
| 240 | |
| 241 | list_protocol->Show(); |
| 242 | |
| 243 | if (visible) surface->EndPaint(); |
| 244 | } |
| 245 | |
| 246 | Void BoCA::LayerProtocols::SaveProtocol() |
| 247 | { |
| 248 | Config *config = Config::Get(); |
| 249 | I18n *i18n = I18n::Get(); |
| 250 | |
| 251 | i18n->SetContext("Extensions::Logging"); |
| 252 | |
| 253 | /* Get protocol. |
| 254 | */ |
| 255 | String displayName = combo_protocol->GetSelectedEntry()->GetText(); |
| 256 | String protocolName = displayName.Tail(displayName.Length() - 11); |
| 257 | |
| 258 | Protocol *protocol = Protocol::Get(protocolName); |
| 259 | |
| 260 | /* Create dialog. |
| 261 | */ |
| 262 | FileSelection dialog; |
| 263 | String fileName = Application::Get()->getClientName.Call().Replace(":", NIL).Append(" ").Append(protocol->GetName().Replace(":", "'")).Append(".log"); |
| 264 | |
| 265 | dialog.SetParentWindow(GetContainerWindow()); |
| 266 | dialog.SetMode(SFM_SAVE); |
| 267 | dialog.SetFlags(SFD_CONFIRMOVERWRITE); |
| 268 | |
| 269 | dialog.AddFilter(i18n->TranslateString("Log Files"), "*.log"); |
| 270 | |
| 271 | dialog.SetFileName(fileName); |
| 272 | dialog.SetInitialPath(config->GetStringValue(ConfigureProtocols::ConfigID, "LastSaveDirectory", NIL)); |
| 273 | |
| 274 | /* Show dialog. |
| 275 | */ |
| 276 | if (dialog.ShowDialog() != Success()) return; |
| 277 | |
| 278 | fileName = dialog.GetFileName(); |
| 279 | |
| 280 | /* Save selected path. |
| 281 | */ |
| 282 | config->SetStringValue(ConfigureProtocols::ConfigID, "LastSaveDirectory", File(fileName).GetFilePath()); |
| 283 | |
| 284 | /* Save log. |
| 285 | */ |
| 286 | ProtocolWriter::SaveProtocol(protocol, fileName); |
| 287 | } |