-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteProperty.pas
469 lines (417 loc) · 15.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
unit RemoteProperty;
interface
uses
Plugin_types, Descriptions, CMLTypes, 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;
DescriptionTS: TTabSheet;
DescriptionEditMemo: TMemo;
DescriptionSaveButton: TButton;
procedure AccessCBClick(Sender: TObject);
class function ShowProperty(parentWindow: HWND; RemoteName: WideString; RemoteProperty: TCloudMailRuDirListingItem; Cloud: TCloudMailRu; DoUrlEncode: Boolean = true; AutoUpdateDownloadListing: Boolean = true; ShowDescription: Boolean = true; EditDescription: Boolean = true; PluginIonFileName: WideString = 'descript.ion'): 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);
procedure DescriptionSaveButtonClick(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
private
{Private declarations}
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 RefreshItemDescription();
procedure SaveItemDescription();
procedure TempPublicCloudInit(publicUrl: WideString);
function LogProc(LogText: WideString): Boolean;
protected
Props: TCloudMailRuDirListingItem;
InvitesListing: TCloudMailRuInviteInfoListing;
Cloud: TCloudMailRu;
RemoteName: WideString;
DoUrlEncode: Boolean;
LogCancelledFlag: Boolean;
AutoUpdateDownloadListing: Boolean;
ShowDescription: Boolean;
EditDescription: Boolean;
PluginIonFileName: WideString; //Переопределённое (или нет) имя файла описаний, с которым будет работать плагин
TempPublicCloud: TCloudMailRu; //Облако для получения прямых ссылок на опубликованные объекты
public
{Public declarations}
end;
var
PropertyForm: TPropertyForm;
implementation
{$R *.dfm}
{TPropertyForm}
procedure TPropertyForm.RefreshItemDescription;
var
CurrentDescriptions: TDescription;
LocalPath: WideString;
begin
DescriptionEditMemo.lines.Clear;
LocalPath := GetTmpFileName('ion');
if not self.Cloud.getDescriptionFile(IncludeTrailingBackslash(ExtractFileDir(self.RemoteName)) + self.PluginIonFileName, LocalPath) then
exit;
CurrentDescriptions := TDescription.Create(LocalPath, GetTCCommentPreferredFormat);
CurrentDescriptions.Read;
DescriptionEditMemo.lines.Text := CurrentDescriptions.GetValue(ExtractFileName(self.RemoteName), FORMAT_CLEAR);
CurrentDescriptions.Destroy;
DeleteFileW(PWideChar(LocalPath));
end;
procedure TPropertyForm.SaveItemDescription;
var
CurrentDescriptions: TDescription;
RemotePath, LocalPath: WideString;
RemoteFileExists: Boolean;
begin
RemotePath := IncludeTrailingBackslash(ExtractFileDir(self.RemoteName)) + self.PluginIonFileName;
LocalPath := GetTmpFileName('ion');
RemoteFileExists := self.Cloud.getDescriptionFile(RemotePath, LocalPath);
CurrentDescriptions := TDescription.Create(LocalPath, GetTCCommentPreferredFormat);
if RemoteFileExists then //если был прежний файл - его надо перечитать и удалить с сервера
begin
CurrentDescriptions.Read;
self.Cloud.deleteFile(RemotePath); //Приходится удалять, потому что не знаем, как переписать
end;
CurrentDescriptions.SetValue(ExtractFileName(self.RemoteName), DescriptionEditMemo.lines.Text);
CurrentDescriptions.Write();
self.Cloud.putDesriptionFile(RemotePath, CurrentDescriptions.ionFilename);
CurrentDescriptions.Destroy;
end;
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(EmptyWideStr, self.TempPublicCloud);
end else begin
DownloadLinksMemo.lines.Text := TempPublicCloud.getSharedFileUrl(EmptyWideStr, self.DoUrlEncode);
end;
TempPublicCloud.Free;
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 := EmptyWideStr;
Props.WebLink := EmptyWideStr;
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;
self.TempPublicCloud := TCloudMailRu.Create(TempAccountSettings, 0, self.Cloud.ProxySettings, self.Cloud.ConnectTimeoutValue, self.Cloud.UploadLimit, self.Cloud.DownloadLimit);
self.TempPublicCloud.login;
end;
function TPropertyForm.LogProc(LogText: WideString): Boolean;
begin
result := not LogCancelledFlag;
if (result) then
LogLabel.Caption := LogText
else
LogLabel.Caption := EmptyWideStr;
LogCancelledFlag := false;
end;
(*Controls handlers*)
procedure TPropertyForm.CancelScanTBClick(Sender: TObject);
begin
LogCancelledFlag := true;
end;
procedure TPropertyForm.DescriptionSaveButtonClick(Sender: TObject);
begin
SaveItemDescription;
end;
procedure TPropertyForm.FormActivate(Sender: TObject);
begin
CenterWindow(self.parentWindow, self.Handle);
end;
procedure TPropertyForm.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_ESCAPE:
Close;
VK_F2:
begin
if (self.ShowDescription) and (self.EditDescription) then
begin
SaveItemDescription;
self.ModalResult := IDCONTINUE;
self.CloseModal;
end;
end;
end;
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 = EmptyWideStr 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; ShowDescription: Boolean = true; EditDescription: Boolean = true; PluginIonFileName: WideString = 'descript.ion'): 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;
PropertyForm.ShowDescription := ShowDescription;
PropertyForm.EditDescription := EditDescription;
PropertyForm.PluginIonFileName := PluginIonFileName;
if ('descript.ion' <> PluginIonFileName) then
PropertyForm.DescriptionTS.Caption := 'Description from ' + PluginIonFileName;
result := PropertyForm.Showmodal;
finally
FreeAndNil(PropertyForm);
end;
end;
procedure TPropertyForm.WMAfterShow(var Message: TMessage);
begin
if not(Props.WebLink = EmptyWideStr) then
begin
WebLink.Text := PUBLIC_ACCESS_URL + Props.WebLink;
WebLink.SetFocus;
WebLink.SelectAll;
end;
ExtPropertiesPC.Visible := false;
FolderAccessTS.TabVisible := false;
DownloadLinksTS.TabVisible := false;
DescriptionTS.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 = EmptyWideStr);
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;
if self.ShowDescription then
begin
ExtPropertiesPC.Visible := true;
DescriptionTS.TabVisible := true;
RefreshItemDescription;
DescriptionEditMemo.ReadOnly := not self.EditDescription;
DescriptionSaveButton.Enabled := self.EditDescription;
end;
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.