File: UQuickConnect.pas

package info (click to toggle)
tuxcmd 0.6.70%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,612 kB
  • ctags: 2,757
  • sloc: pascal: 49,754; ansic: 2,272; makefile: 106
file content (360 lines) | stat: -rw-r--r-- 13,782 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
(*
    Tux Commander - UQuickConnect - Quick connection dialog
    Copyright (C) 2008 Tomas Bzatek <tbzatek@users.sourceforge.net>
    Check for updates on tuxcmd.sourceforge.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)
unit UQuickConnect;

interface

uses
  glib2, gdk2, gtk2, pango, SysUtils, Types, Classes, GTKControls, GTKForms, GTKStdCtrls, GTKExtCtrls, GTKConsts, GTKView,
  GTKUtils, GTKDialogs, GTKPixbuf, GTKClasses, GTKMenus,
  UCore, UCoreClasses, UVFSCore, UEngines, UConnectionManager;

type
  TFQuickConnect = class(TGTKDialog)
    TitleFrame: TGTKFrame;
    TitleLabel: TGTKLabel;
    TitleEventBox: TGTKEventBox;
    TitleIcon: TGTKImage;
    TitleHBox: TGTKHBox;
    Table: TGTKTable;
    ActionButtonBox: TGTKHButtonBox;
    ConnectButton, StopButton, CloseButton: TGTKButton;
    Label1, Label2: TGTKLabel;
    URIComboBox: TGTKCombo;
    PluginOptionMenu: TGTKOptionMenu;
    procedure FormCreate(Sender: TObject); override;
    procedure FormKeyDown(Sender: TObject; Key: Word; Shift: TShiftState; var Accept: boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormResponse(Sender: TObject; const ResponseID: integer);
    procedure CloseButtonClick(Sender: TObject);
    procedure ConnectButtonClick(Sender: TObject);
    procedure StopButtonClick(Sender: TObject);
    procedure ComboBoxKeyDown(Sender: TObject; Key: Word; Shift: TShiftState; var Accept: boolean);
    procedure ComboBoxChanged(Sender: TObject);
  private
    AFConnectionManager: TFConnectionManager;
    SavedData: string;
    procedure DoConnect;
  public
    SourcePanelEngine: TPanelEngine;
    ConnectedEngine: TVFSEngine;
    ConnectionManager: TFConnectionManager;
  end;

var
  FQuickConnect: TFQuickConnect;

implementation

uses ULocale, UCoreUtils, UConfig, UConnectionProperties, UGnome, uVFSprototypes;


procedure TFQuickConnect.FormCreate(Sender: TObject);
var i: integer;
    MenuItem: TGTKMenuItem;
begin
  SavedData := '';
  ConnectedEngine := nil;
  ConnectionManager := nil;
  AFConnectionManager := nil;
  SetDefaultSize(435, 200);
  Caption := LANGFQuickConnect_Caption;
  Buttons := [];
  ShowSeparator := False;

  ConnectButton := TGTKButton.Create(Self);
  ConnectButton.Caption := LANGConnMgr_ConnectButton;
//  ConnectButton.Default := True;
  CloseButton := TGTKButton.CreateFromStock(Self, GTK_STOCK_CLOSE);
  StopButton := TGTKButton.CreateFromStock(Self, GTK_STOCK_STOP);
  StopButton.Visible := False;
  StopButton.Enabled := False;
//  Default := ConnectButton;
  ActionButtonBox := TGTKHButtonBox.Create(Self);
  ActionButtonBox.Layout := blEnd;
  ActionButtonBox.Spacing := 10;
  ActionButtonBox.BorderWidth := 0;
  ActionButtonBox.AddControlEnd(CloseButton);
  ActionButtonBox.AddControlEnd(ConnectButton);
  ActionButtonBox.AddControlEnd(StopButton);

  ActionArea.AddControlEx(ActionButtonBox, False, False, 0);

  TitleEventBox := TGTKEventBox.Create(Self);
  TitleLabel := TGTKLabel.Create(Self);
  TitleLabel.Caption := Format('<span size="x-large" weight="ultrabold">%s</span>', [LANGFQuickConnect_TitleLabel_Caption]);
  TitleLabel.UseMarkup := True;
  TitleLabel.XAlign := 0;
  TitleLabel.XPadding := 0;
  TitleLabel.YPadding := 3;
  TitleEventBox.ControlState := csPrelight;
  TitleFrame := TGTKFrame.CreateWithoutLabel(Self);
  TitleFrame.ShadowType := stShadowOut;
  TitleIcon := TGTKImage.Create(Self);
  TitleIcon.SetFromStock('gtk-connect', isLargeToolbar);
  TitleHBox := TGTKHBox.Create(Self);
  TitleHBox.Homogeneous := False;
  TitleHBox.AddControlEx(TGTKEventBox.Create(Self), False, False, 5);
  TitleHBox.AddControlEx(TitleIcon, False, False, 0);
  TitleHBox.AddControlEx(TitleLabel, True, True, 10);
  TitleEventBox.AddControl(TitleHBox);
  TitleFrame.AddControl(TitleEventBox);
  ClientArea.AddControlEx(TitleFrame, False, True, 0);

  Table := TGTKTable.Create(Self);
  Table.BorderWidth := 25;
  ClientArea.AddControlEx(Table, True, True, 0);

  URIComboBox := TGtkCombo.Create(Self);
  URIComboBox.DisableActivate;
  PluginOptionMenu := TGTKOptionMenu.Create(Self);

  Label1 := TGTKLabel.Create(Self);
  Label1.Caption := Format('<span weight="ultrabold">%s</span>', [LANGFQuickConnect_ConnectToURILabel_Caption]);
  Label1.FocusControl := URIComboBox.Entry;
  Label1.UseMarkup := True;
  Label1.UseUnderline := True;
  Label1.XAlign := 0;
  Label2 := TGTKLabel.Create(Self);
  Label2.Caption := LANGConnProp_VFSModule;
  Label2.FocusControl := PluginOptionMenu;
  Label2.UseUnderline := True;
  Label2.XAlign := 0;

  if QuickConnectHistory.Count > 0 then
    for i := 0 to QuickConnectHistory.Count - 1 do
      URIComboBox.Items.Append(QuickConnectHistory[i]);
  URIComboBox.Entry.Text := '';

  //  Fill the plugins menu
  MenuItem := TGTKMenuItem.CreateTyped(Self, itLabel);
  MenuItem.Caption := LANGConnProp_MenuItemCaption;
  PluginOptionMenu.Items.Add(MenuItem);
  for i := 0 to PluginList.Count - 1 do
    if TVFSPlugin(PluginList[i]).HandlesNetwork then begin
      MenuItem := TGTKMenuItem.CreateTyped(Self, itImageText);
      MenuItem.Data := PluginList[i];
      MenuItem.SetCaptionPlain(Format('%s [%s]', [TVFSPlugin(PluginList[i]).ModuleName,
                                                  ExtractFileName(TVFSPlugin(PluginList[i]).FullModulePath)]));
      PluginOptionMenu.Items.Add(MenuItem);
    end;

  //  Find last used plugin
  if (Length(Trim(ConfQuickConnectPluginID)) > 0) and (PluginOptionMenu.Items.Count > 0) then
    for i := 0 to PluginOptionMenu.Items.Count - 1 do
      if (PluginOptionMenu.Items[i].Data <> nil) and (TVFSPlugin(PluginOptionMenu.Items[i].Data).ModuleID = ConfQuickConnectPluginID) then begin
        PluginOptionMenu.ItemIndex := i;
        Break;
      end;

//  Table.AddControlEx(1, 1, 2, 1, TGTKEventBox.Create(Self), [taoExpand, taoFill], [taoShrink], 0, 2);
  Table.AddControlEx(0, 0, 2, 1, Label1, [taoShrink, taoFill], [taoShrink], 0, 2);
  Table.AddControlEx(0, 1, 2, 1, URIComboBox, [taoExpand, taoFill], [taoShrink], 0, 5);
  Table.AddControlEx(0, 2, 1, 1, Label2, [taoShrink, taoFill], [taoShrink], 10, 5);
  Table.AddControlEx(1, 2, 1, 1, PluginOptionMenu, [taoExpand, taoFill], [taoShrink], 0, 5);

  CloseButton.OnClick := CloseButtonClick;
  ConnectButton.OnClick := ConnectButtonClick;
  StopButton.OnClick := StopButtonClick;
  OnKeyDown := FormKeyDown;
  OnClose := FormClose;
  OnResponse := FormResponse;
  URIComboBox.Entry.OnKeyDown := ComboBoxKeyDown;
  URIComboBox.Entry.OnChanged := ComboBoxChanged;
  ComboBoxChanged(Sender);
end;

procedure TFQuickConnect.FormKeyDown(Sender: TObject; Key: Word; Shift: TShiftState; var Accept: boolean);
begin
  case Key of
    GDK_RETURN, GDK_KP_ENTER: if StopButton.Visible then StopButtonClick(Sender)
                                                    else if ConnectButton.Enabled then DoConnect;
    GDK_ESCAPE: begin
                  Accept := False;
                  if StopButton.Visible then StopButtonClick(Sender)
                                        else ModalResult := mbCancel;
                end;
  end;
end;

procedure TFQuickConnect.ComboBoxKeyDown(Sender: TObject; Key: Word; Shift: TShiftState; var Accept: boolean);
var Orig, s: string;
    i: integer;
begin
  case Key of
    GDK_UP, GDK_DOWN: if Shift = [] then begin
                        Accept := False;
                        if QuickConnectHistory.Count > 0 then begin
                          Orig := Trim(URIComboBox.Entry.Text);
                          i := QuickConnectHistory.IndexOf(Orig);

                          if Key = GDK_DOWN then begin
                            if i < 0 then begin
                              SavedData := Orig;
                              i := 0;
                            end else
                            if QuickConnectHistory.Count > i + 1 then Inc(i);
                            s := QuickConnectHistory[i];
                          end else begin
                            if i < 0 then Exit else
                            if i = 0 then begin
                              s := SavedData;
                              SavedData := '';
                            end else
                            if QuickConnectHistory.Count > i then s := QuickConnectHistory[i - 1];
                          end;

                          URIComboBox.Entry.Text := s;
                          URIComboBox.Entry.SetFocus;
                          URIComboBox.Entry.SelectAll;
                        end;
                      end;
  end;
end;

procedure TFQuickConnect.ComboBoxChanged(Sender: TObject);
begin
  ConnectButton.Enabled := Length(Trim(URIComboBox.Entry.Text)) > 0;
end;

procedure TFQuickConnect.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Length(Trim(URIComboBox.Entry.Text)) > 0 then
    SaveItemToHistory(URIComboBox.Entry.Text, QuickConnectHistory);
  if (PluginOptionMenu.ItemIndex <> 0) and (PluginOptionMenu.Items[PluginOptionMenu.ItemIndex].Data <> nil)
    then ConfQuickConnectPluginID := TVFSPlugin(PluginOptionMenu.Items[PluginOptionMenu.ItemIndex].Data).ModuleID
    else ConfQuickConnectPluginID := '';
end;

procedure TFQuickConnect.FormResponse(Sender: TObject; const ResponseID: integer);
var Action: TCloseAction;
begin
  FormClose(Sender, Action);
end;

(********************************************************************************************************************************)
procedure TFQuickConnect.CloseButtonClick(Sender: TObject);
begin
  ModalResult := mbCancel;
end;

procedure TFQuickConnect.ConnectButtonClick(Sender: TObject);
begin
  DoConnect;
end;

procedure TFQuickConnect.StopButtonClick(Sender: TObject);
begin
  if (AFConnectionManager <> nil) and (AFConnectionManager.Thread <> nil) then
    AFConnectionManager.Thread.FCancelRequested := True;
end;

procedure TFQuickConnect.DoConnect;
var Engine: TVFSEngine;
    i, j: integer;
    VFSPlugin: TVFSPlugin;
    URI, Scheme: string;
    DontShowAgain: boolean;
    res: TMessageButton;
begin
  if ConnectionManager <> nil then AFConnectionManager := ConnectionManager
                              else AFConnectionManager := TFConnectionManager.Create(Self);

  if PluginList.Count = 0 then begin
    Application.MessageBox(PGtkWindow(FWidget), LANGThereIsNoModuleAvailable, [mbOK], mbError, mbOK, mbOK);
    Exit;
  end;

  //  Find VFS module to use for this connection
  VFSPlugin := nil;
  if (PluginOptionMenu.ItemIndex <> 0) and (PluginOptionMenu.Items[PluginOptionMenu.ItemIndex].Data <> nil) then begin
    VFSPlugin := PluginOptionMenu.Items[PluginOptionMenu.ItemIndex].Data;
    ConfQuickConnectPluginID := VFSPlugin.ModuleID;
  end else ConfQuickConnectPluginID := '';
  if VFSPlugin = nil then begin
    Scheme := '';
    if Pos('://', URIComboBox.Entry.Text) > 0 then
      Scheme := Copy(URIComboBox.Entry.Text, 1, Pos('://', URIComboBox.Entry.Text) - 1);
    for i := 0 to PluginList.Count - 1 do begin
      if Length(TVFSPlugin(PluginList[i]).Services) > 0 then
        for j := 0 to Length(TVFSPlugin(PluginList[i]).Services) - 1 do
          if WideCompareText(TVFSPlugin(PluginList[i]).Services[j], Scheme) = 0 then begin
            VFSPlugin := PluginList[i];
            Break;
          end;
      if VFSPlugin <> nil then Break;
    end;
    if VFSPlugin = nil then VFSPlugin := PluginList[0];     //  Fallback in hope some other plugin can handle it
  end;

  if (SourcePanelEngine is TVFSEngine) and ConfReplaceConnectionWarning then begin
    DontShowAgain := False;
    res := MessageBoxShowOnce(PGtkWindow(FWidget), LANGCloseOpenConnection, LANGDontShowAgain, DontShowAgain, [mbYes, mbNo], mbWarning, mbYes, mbNo);
    if DontShowAgain then begin
      ConfReplaceConnectionWarning := False;
      WriteMainGUISettings;
    end;
    if res <> mbYes then Exit;
  end;

  Table.Enabled := False;
  CloseButton.Enabled := False;
  ConnectButton.Enabled := False;
  ConnectButton.Visible := False;
  StopButton.Enabled := True;
  StopButton.Visible := True;
  StopButton.Default := True;
  StopButton.SetFocus;

  //  Construct the VFS Engine and try to open the connection
  Engine := TVFSEngine.Create(VFSPlugin);
  Engine.ParentEngine := SourcePanelEngine;
  Engine.SavePath := SourcePanelEngine.Path;
  Engine.OpenedFromQuickConnect := True;
  Engine.CustomPluginIDSave := ConfQuickConnectPluginID;
  URI := URIComboBox.Entry.Text;
  Engine.Password := URIRipPassword(URI, True);

  if not AFConnectionManager.DoConnectInternal(URI, Engine, FWidget, True) then begin
    if not AFConnectionManager.FSilenceError then Application.MessageBox(PGtkWindow(FWidget), LANGCouldntOpenURI, [mbOK], mbError, mbOK, mbOK);
    Table.Enabled := True;
    CloseButton.Enabled := True;
    ConnectButton.Enabled := True;
    ConnectButton.Visible := True;
    StopButton.Visible := False;
    StopButton.Enabled := False;
    URIComboBox.Entry.SetFocus;
    Engine.Free;
    Exit;
  end;

  Engine.Password := '';   //  Security precaution
  StopButton.Enabled := False;
  ConnectedEngine := Engine;

  if ConnectionManager = nil then AFConnectionManager.Free;
  ModalResult := mbOK;
end;



end.