forked from pozitronik/CloudMailRu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteProperty.pas
388 lines (343 loc) · 12.7 KB
/
RemoteProperty.pas
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
unit RemoteProperty;
interface
uses
Plugin_types, Settings, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CloudMailRu, MRC_Helper, Vcl.Grids, Vcl.ValEdit, Vcl.Menus, Vcl.ComCtrls, Vcl.ToolWin, System.ImageList, Vcl.ImgList;
const
WM_AFTER_SHOW = WM_USER + 300; //custom message
type
TPropertyForm = class(TForm)
PublicLinkLabel: TLabel;
WebLink: TEdit;
AccessCB: TCheckBox;
OkButton: TButton;
InvitesPopup: TPopupMenu;
ItemChangeAccess: TMenuItem;
ItemDelete: TMenuItem;
ItemRefresh: TMenuItem;
N1: TMenuItem;
ExtPropertiesPC: TPageControl;
FolderAccessTS: TTabSheet;
DownloadLinksTS: TTabSheet;
InviteEmailLabel: TLabel;
AccessLabel: TLabel;
InviteEmailEdit: TEdit;
InviteAcessCB: TComboBox;
InviteBtn: TButton;
InvitesLE: TValueListEditor;
DownloadLinksMemo: TMemo;
DownloadLinksTB: TToolBar;
SaveBtn: TToolButton;
DownloadLinksIL: TImageList;
WrapBTN: TToolButton;
DownloadLinksSD: TSaveDialog;
LogLabel: TLabel;
CancelScanTB: TToolButton;
RefreshScanTB: TToolButton;
procedure AccessCBClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
class function ShowProperty(parentWindow: HWND; RemoteName: WideString; RemoteProperty: TCloudMailRuDirListingItem; Cloud: TCloudMailRu; DoUrlEncode: Boolean = true; AutoUpdateDownloadListing: Boolean = true): Integer;
procedure FormActivate(Sender: TObject);
procedure InviteBtnClick(Sender: TObject);
procedure ItemDeleteClick(Sender: TObject);
procedure ItemRefreshClick(Sender: TObject);
procedure InvitesPopupPopup(Sender: TObject);
procedure ItemChangeAccessClick(Sender: TObject);
procedure WrapBTNClick(Sender: TObject);
procedure SaveBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure CancelScanTBClick(Sender: TObject);
procedure RefreshScanTBClick(Sender: TObject);
private
{Private declarations}
procedure WMHotKey(var Message: TMessage); message WM_HOTKEY;
procedure WMAfterShow(var Message: TMessage); message WM_AFTER_SHOW;
procedure RefreshInvites();
procedure RefreshPublicShare(const Publish: Boolean);
function FillRecursiveDownloadListing(const Path: WideString; Cloud: TCloudMailRu = nil): Boolean; //break recursion if false - cancelled
procedure UpdateDownloadListing();
procedure TempPublicCloudInit(publicUrl: WideString);
function LogProc(LogText: WideString): Boolean;
protected
Props: TCloudMailRuDirListingItem;
InvitesListing: TCloudMailRuInviteInfoListing;
Cloud: TCloudMailRu;
RemoteName: WideString;
DoUrlEncode: Boolean;
LogCancelledFlag: Boolean;
AutoUpdateDownloadListing: Boolean;
TempPublicCloud: TCloudMailRu; //Облако для получения прямых ссылок на опубликованные объекты
public
{Public declarations}
end;
var
PropertyForm: TPropertyForm;
implementation
{$R *.dfm}
{TPropertyForm}
(*Class methods*)
procedure TPropertyForm.UpdateDownloadListing;
begin
DownloadLinksMemo.Lines.Clear;
if self.Cloud.isPublicShare then
begin
if Props.type_ = TYPE_DIR then
begin (*рекурсивно получаем все ссылки в каталоге*)
FillRecursiveDownloadListing(IncludeTrailingPathDelimiter(self.RemoteName))
end else begin
DownloadLinksMemo.Lines.Text := self.Cloud.getSharedFileUrl(self.RemoteName, self.DoUrlEncode);
end;
end else begin
(*У объекта есть публичная ссылка, можно получить прямые ссылки на скачивание*)
TempPublicCloudInit(WebLink.Text);
if Props.type_ = TYPE_DIR then
begin (*рекурсивно получаем все ссылки в каталоге*)
FillRecursiveDownloadListing('', self.TempPublicCloud);
end else begin
DownloadLinksMemo.Lines.Text := TempPublicCloud.getSharedFileUrl('', self.DoUrlEncode);
end;
TempPublicCloud.Free;
{+}
TempPublicCloud := nil;
{+.}
end;
LogProc('Done');
end;
function TPropertyForm.FillRecursiveDownloadListing(const Path: WideString; Cloud: TCloudMailRu = nil): Boolean;
var
CurrentDirListing: TCloudMailRuDirListing;
CurrentDirItemsCounter: Integer;
begin
CancelScanTB.Enabled := true;
RefreshScanTB.Enabled := false;
result := true;
if not(Assigned(Cloud)) then Cloud := self.Cloud;
if not LogProc('Scanning ' + IncludeTrailingPathDelimiter(Path)) then exit(false);
Cloud.getDirListing(Path, CurrentDirListing);
ProcessMessages;
for CurrentDirItemsCounter := 0 to length(CurrentDirListing) - 1 do
begin
if CurrentDirListing[CurrentDirItemsCounter].type_ = TYPE_DIR then
begin
result := FillRecursiveDownloadListing(IncludeTrailingPathDelimiter(Path) + CurrentDirListing[CurrentDirItemsCounter].name, Cloud);
if not result then break;
end else begin
DownloadLinksMemo.Lines.Add(Cloud.getSharedFileUrl(IncludeTrailingPathDelimiter(Path) + CurrentDirListing[CurrentDirItemsCounter].name, self.DoUrlEncode));
end;
end;
RefreshScanTB.Enabled := true;
CancelScanTB.Enabled := false;
end;
procedure TPropertyForm.RefreshPublicShare(const Publish: Boolean);
var
PublicLink: WideString;
begin
if Publish then
begin
if self.Cloud.publishFile(Props.home, PublicLink) then
begin
WebLink.Text := PUBLIC_ACCESS_URL + PublicLink;
Props.WebLink := PublicLink;
WebLink.Enabled := true;
WebLink.SetFocus;
WebLink.SelectAll;
ExtPropertiesPC.Visible := true;
DownloadLinksTS.TabVisible := true;
//UpdateDownloadListing;
end else begin
MessageBoxW(self.Handle, PWideChar('Error while publishing file ' + Props.home + ', see main log'), 'File publishing error', MB_OK + MB_ICONERROR);
end;
end else begin
if Cloud.publishFile(Props.home, Props.WebLink, CLOUD_UNPUBLISH) then
begin
WebLink.Text := '';
Props.WebLink := '';
WebLink.Enabled := false;
DownloadLinksTS.TabVisible := false;
if ExtPropertiesPC.TabIndex = -1 then ExtPropertiesPC.Visible := false;
end else begin
MessageBoxW(self.Handle, PWideChar('Error while unpublishing file ' + Props.home + ', see main log'), 'File unpublishing error', MB_OK + MB_ICONERROR);
end;
end;
end;
procedure TPropertyForm.RefreshScanTBClick(Sender: TObject);
begin
UpdateDownloadListing;
end;
procedure TPropertyForm.RefreshInvites;
var
i, InvitesCount: Integer;
begin
while InvitesLE.Strings.Count > 0 do InvitesLE.DeleteRow(1);
if Cloud.getShareInfo(Props.home, self.InvitesListing) then
begin
InvitesCount := length(self.InvitesListing) - 1;
for i := 0 to InvitesCount do InvitesLE.InsertRow(self.InvitesListing[i].email, TCloudMailRu.CloudAccessToString(self.InvitesListing[i].access), true);
end else begin
MessageBoxW(self.Handle, PWideChar('Error while retrieving ' + Props.home + ' folder invites list, see main log'), 'Folder invite listing error', MB_OK + MB_ICONERROR);
end;
end;
procedure TPropertyForm.TempPublicCloudInit(publicUrl: WideString);
var
TempAccountSettings: TAccountSettings;
begin
TempAccountSettings.public_account := true;
TempAccountSettings.public_url := publicUrl;
{+}
FreeAndNil(TempPublicCloud);
{+.}
self.TempPublicCloud := TCloudMailRu.Create(TempAccountSettings, 0, self.Cloud.ProxySettings, self.Cloud.ConnectTimeoutValue);
self.TempPublicCloud.login;
end;
function TPropertyForm.LogProc(LogText: WideString): Boolean;
begin
result := not LogCancelledFlag;
if (result) then LogLabel.Caption := LogText
else LogLabel.Caption := '';
LogCancelledFlag := false;
end;
(*Controls handlers*)
procedure TPropertyForm.CancelScanTBClick(Sender: TObject);
begin
LogCancelledFlag := true;
end;
procedure TPropertyForm.FormActivate(Sender: TObject);
begin
CenterWindow(self.parentWindow, self.Handle);
end;
procedure TPropertyForm.FormDestroy(Sender: TObject);
begin
UnregisterHotKey((Sender as TPropertyForm).Handle, 1)
end;
procedure TPropertyForm.FormShow(Sender: TObject);
begin
PostMessage(self.Handle, WM_AFTER_SHOW, 0, 0);
end;
procedure TPropertyForm.InviteBtnClick(Sender: TObject);
begin
if (Cloud.shareFolder(Props.home, InviteEmailEdit.Text, InviteAcessCB.ItemIndex)) then
begin
RefreshInvites;
end else begin
MessageBoxW(self.Handle, PWideChar('Error while inviting ' + InviteEmailEdit.Text + ' to ' + Props.home + ' folder, see main log'), 'Folder invite error', MB_OK + MB_ICONERROR);
end;
end;
procedure TPropertyForm.InvitesPopupPopup(Sender: TObject);
var
email, access: WideString;
begin
email := InvitesLE.Keys[InvitesLE.Row];
if email = '' then
begin
ItemChangeAccess.Visible := false;
ItemDelete.Visible := false;
exit;
end else begin
ItemChangeAccess.Visible := true;
ItemDelete.Visible := true;
end;
access := InvitesLE.Values[email];
access := TCloudMailRu.CloudAccessToString(access, true);
ItemChangeAccess.Caption := 'Change access to ' + access;
end;
procedure TPropertyForm.ItemChangeAccessClick(Sender: TObject);
var
email, access: WideString;
begin
email := InvitesLE.Keys[InvitesLE.Row];
access := InvitesLE.Values[email];
if Cloud.shareFolder(Props.home, InvitesLE.Keys[InvitesLE.Row], TCloudMailRu.StringToCloudAccess(access, true)) then
begin
RefreshInvites;
end else begin
MessageBoxW(self.Handle, PWideChar('Error while removing access to ' + InviteEmailEdit.Text + ' from ' + Props.home + ' folder, see main log'), 'Folder unshare error', MB_OK + MB_ICONERROR);
end;
end;
procedure TPropertyForm.ItemDeleteClick(Sender: TObject);
begin
if Cloud.shareFolder(Props.home, InvitesLE.Keys[InvitesLE.Row], CLOUD_SHARE_NO) then
begin
RefreshInvites;
end else begin
MessageBoxW(self.Handle, PWideChar('Error while removing access to ' + InviteEmailEdit.Text + ' from ' + Props.home + ' folder, see main log'), 'Folder unshare error', MB_OK + MB_ICONERROR);
end;
end;
procedure TPropertyForm.ItemRefreshClick(Sender: TObject);
begin
RefreshInvites;
end;
procedure TPropertyForm.SaveBtnClick(Sender: TObject);
begin
if (DownloadLinksSD.Execute(self.Handle)) then
begin
DownloadLinksMemo.Lines.SaveToFile(DownloadLinksSD.FileName);
end;
end;
class function TPropertyForm.ShowProperty(parentWindow: HWND; RemoteName: WideString; RemoteProperty: TCloudMailRuDirListingItem; Cloud: TCloudMailRu; DoUrlEncode: Boolean = true; AutoUpdateDownloadListing: Boolean = true): Integer;
var
PropertyForm: TPropertyForm;
begin
try
PropertyForm := TPropertyForm.Create(nil);
PropertyForm.parentWindow := parentWindow;
PropertyForm.RemoteName := RemoteName;
PropertyForm.Caption := RemoteProperty.name;
PropertyForm.Cloud := Cloud;
PropertyForm.Props := RemoteProperty;
PropertyForm.AutoUpdateDownloadListing := AutoUpdateDownloadListing;
PropertyForm.LogCancelledFlag := false;
PropertyForm.DoUrlEncode := DoUrlEncode;
RegisterHotKey(PropertyForm.Handle, 1, 0, VK_ESCAPE);
result := PropertyForm.Showmodal;
finally
FreeAndNil(PropertyForm);
end;
end;
procedure TPropertyForm.WMAfterShow(var Message: TMessage);
begin
if not(Props.WebLink = '') then
begin
WebLink.Text := PUBLIC_ACCESS_URL + Props.WebLink;
WebLink.SetFocus;
WebLink.SelectAll;
end;
ExtPropertiesPC.Visible := false;
FolderAccessTS.TabVisible := false;
DownloadLinksTS.TabVisible := false;
if self.Cloud.isPublicShare then
begin
AccessCB.Enabled := false;
AccessCB.checked := true;
ExtPropertiesPC.Visible := true;
DownloadLinksTS.TabVisible := true;
if self.AutoUpdateDownloadListing then UpdateDownloadListing;
end else begin
AccessCB.checked := not(Props.WebLink = '');
WebLink.Enabled := AccessCB.checked;
if (Props.type_ = TYPE_DIR) or (Props.kind = KIND_SHARED) then
begin
ExtPropertiesPC.Visible := true;
FolderAccessTS.TabVisible := true;
RefreshInvites;
end;
end;
end;
procedure TPropertyForm.WMHotKey(var Message: TMessage);
begin
if (Message.LParamHi = VK_ESCAPE) and (GetForegroundWindow = self.Handle) then Close;
end;
procedure TPropertyForm.WrapBTNClick(Sender: TObject);
begin
if WrapBTN.Down then DownloadLinksMemo.ScrollBars := ssVertical
else DownloadLinksMemo.ScrollBars := ssBoth;
DownloadLinksMemo.WordWrap := WrapBTN.Down;
end;
procedure TPropertyForm.AccessCBClick(Sender: TObject);
begin
if self.Cloud.isPublicShare then exit;
AccessCB.Enabled := false; //блокируем во избежание повторных кликов
WebLink.Text := 'Wait for it...';
RefreshPublicShare(AccessCB.checked);
if AccessCB.checked and self.AutoUpdateDownloadListing then UpdateDownloadListing;
AccessCB.Enabled := true;
end;
end.