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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
// Far.cpp
// Test Align for updating !!!!!!!!!!!!!!!!!!
#include "StdAfx.h"
#include "../../../Common/MyInitGuid.h"
#include "../../../Common/StringConvert.h"
#include "../../../Windows/FileDir.h"
#include "../../../Windows/NtCheck.h"
#include "../../Common/FileStreams.h"
#include "Messages.h"
#include "Plugin.h"
#include "ProgressBox.h"
using namespace NWindows;
using namespace NFile;
using namespace NDir;
using namespace NFar;
static const DWORD kShowProgressTime_ms = 100;
static const char *kCommandPrefix = "7-zip";
static const TCHAR *kRegisrtryMainKeyName = TEXT("");
static const TCHAR *kRegisrtryValueNameEnabled = TEXT("UsedByDefault3");
static const char *kHelpTopicConfig = "Config";
static bool kPluginEnabledDefault = true;
HINSTANCE g_hInstance;
namespace NFar {
const char *g_PluginName_for_Error = "7-Zip";
}
#define NT_CHECK_FAIL_ACTION return FALSE;
BOOL WINAPI DllMain(
#ifdef UNDER_CE
HANDLE
#else
HINSTANCE
#endif
hInstance, DWORD dwReason, LPVOID)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
// OutputDebugStringA("7-Zip FAR DLL_PROCESS_ATTACH");
g_hInstance = (HINSTANCE)hInstance;
NT_CHECK
}
if (dwReason == DLL_PROCESS_DETACH)
{
// OutputDebugStringA("7-Zip FAR DLL_PROCESS_DETACH");
}
return TRUE;
}
static struct COptions
{
bool Enabled;
} g_Options;
static const TCHAR *kPliginNameForRegestry = TEXT("7-ZIP");
EXTERN_C void WINAPI ExitFAR()
{
/* WIN32:
it's not allowed to call FreeLibrary() from FreeLibrary().
So we try to free all DLLs before destructors */
// OutputDebugStringA("-- ExitFAR --- START");
FreeGlobalCodecs();
// OutputDebugStringA("-- ExitFAR --- END");
}
EXTERN_C void WINAPI SetStartupInfo(const PluginStartupInfo *info)
{
MY_TRY_BEGIN;
g_StartupInfo.Init(*info, kPliginNameForRegestry);
g_Options.Enabled = g_StartupInfo.QueryRegKeyValue(
HKEY_CURRENT_USER, kRegisrtryMainKeyName,
kRegisrtryValueNameEnabled, kPluginEnabledDefault);
// OutputDebugStringA("SetStartupInfo");
// LoadGlobalCodecs();
MY_TRY_END1("SetStartupInfo");
}
class COpenArchiveCallback:
public IArchiveOpenCallback,
public IArchiveOpenVolumeCallback,
public IProgress,
public ICryptoGetTextPassword,
public CMyUnknownImp
{
DWORD m_StartTickValue;
bool m_MessageBoxIsShown;
CProgressBox _progressBox;
bool _numFilesTotalDefined;
bool _numBytesTotalDefined;
NFind::CFileInfo _fileInfo;
public:
bool PasswordIsDefined;
UString Password;
FString _folderPrefix;
public:
MY_UNKNOWN_IMP3(
IArchiveOpenVolumeCallback,
IProgress,
ICryptoGetTextPassword
)
// IProgress
STDMETHOD(SetTotal)(UInt64 total);
STDMETHOD(SetCompleted)(const UInt64 *aCompleteValue);
// IArchiveOpenCallback
STDMETHOD(SetTotal)(const UInt64 *numFiles, const UInt64 *numBytes);
STDMETHOD(SetCompleted)(const UInt64 *numFiles, const UInt64 *numBytes);
// IArchiveOpenVolumeCallback
STDMETHOD(GetProperty)(PROPID propID, PROPVARIANT *value);
STDMETHOD(GetStream)(const wchar_t *name, IInStream **inStream);
// ICryptoGetTextPassword
STDMETHOD(CryptoGetTextPassword)(BSTR *password);
void Init()
{
PasswordIsDefined = false;
_numFilesTotalDefined = false;
_numBytesTotalDefined = false;
m_MessageBoxIsShown = false;
_progressBox.Init(
// g_StartupInfo.GetMsgString(NMessageID::kWaitTitle),
g_StartupInfo.GetMsgString(NMessageID::kReading));
}
void ShowMessage();
void LoadFileInfo(const FString &folderPrefix, const FString &fileName)
{
_folderPrefix = folderPrefix;
if (!_fileInfo.Find(_folderPrefix + fileName))
throw 1;
}
};
static HRESULT CheckBreak2()
{
return WasEscPressed() ? E_ABORT : S_OK;
}
void COpenArchiveCallback::ShowMessage()
{
if (!m_MessageBoxIsShown)
{
DWORD currentTime = GetTickCount();
if (currentTime - _progressBox.StartTick < kShowProgressTime_ms)
return;
m_MessageBoxIsShown = true;
}
_progressBox.UseBytesForPercents = !_numFilesTotalDefined;
_progressBox.Print();
}
STDMETHODIMP COpenArchiveCallback::SetTotal(const UInt64 *numFiles, const UInt64 *numBytes)
{
_numFilesTotalDefined = (numFiles != NULL);
if (_numFilesTotalDefined)
_progressBox.FilesTotal = *numFiles;
_numBytesTotalDefined = (numBytes != NULL);
if (_numBytesTotalDefined)
_progressBox.Total = *numBytes;
return CheckBreak2();
}
STDMETHODIMP COpenArchiveCallback::SetCompleted(const UInt64 *numFiles, const UInt64 *numBytes)
{
if (numFiles)
_progressBox.Files = *numFiles;
if (numBytes)
_progressBox.Completed = *numBytes;
ShowMessage();
return CheckBreak2();
}
STDMETHODIMP COpenArchiveCallback::SetTotal(const UInt64 /* total */)
{
return CheckBreak2();
}
STDMETHODIMP COpenArchiveCallback::SetCompleted(const UInt64 * /* completed */)
{
ShowMessage();
return CheckBreak2();
}
STDMETHODIMP COpenArchiveCallback::GetStream(const wchar_t *name, IInStream **inStream)
{
if (WasEscPressed())
return E_ABORT;
*inStream = NULL;
FString fullPath = _folderPrefix + us2fs(name);
if (!_fileInfo.Find(fullPath))
return S_FALSE;
if (_fileInfo.IsDir())
return S_FALSE;
CInFileStream *inFile = new CInFileStream;
CMyComPtr<IInStream> inStreamTemp = inFile;
if (!inFile->Open(fullPath))
return ::GetLastError();
*inStream = inStreamTemp.Detach();
return S_OK;
}
STDMETHODIMP COpenArchiveCallback::GetProperty(PROPID propID, PROPVARIANT *value)
{
NCOM::CPropVariant prop;
switch(propID)
{
case kpidName: prop = GetUnicodeString(_fileInfo.Name, CP_OEMCP); break;
case kpidIsDir: prop = _fileInfo.IsDir(); break;
case kpidSize: prop = _fileInfo.Size; break;
case kpidAttrib: prop = (UInt32)_fileInfo.Attrib; break;
case kpidCTime: prop = _fileInfo.CTime; break;
case kpidATime: prop = _fileInfo.ATime; break;
case kpidMTime: prop = _fileInfo.MTime; break;
}
prop.Detach(value);
return S_OK;
}
HRESULT GetPassword(UString &password)
{
if (WasEscPressed())
return E_ABORT;
password.Empty();
CInitDialogItem initItems[]=
{
{ DI_DOUBLEBOX, 3, 1, 72, 4, false, false, 0, false, NMessageID::kGetPasswordTitle, NULL, NULL },
{ DI_TEXT, 5, 2, 0, 0, false, false, DIF_SHOWAMPERSAND, false, NMessageID::kEnterPasswordForFile, NULL, NULL },
{ DI_PSWEDIT, 5, 3, 70, 3, true, false, 0, true, -1, "", NULL }
};
const int kNumItems = ARRAY_SIZE(initItems);
FarDialogItem dialogItems[kNumItems];
g_StartupInfo.InitDialogItems(initItems, dialogItems, kNumItems);
// sprintf(DialogItems[1].Data,GetMsg(MGetPasswordForFile),FileName);
if (g_StartupInfo.ShowDialog(76, 6, NULL, dialogItems, kNumItems) < 0)
return E_ABORT;
AString oemPassword = dialogItems[2].Data;
password = MultiByteToUnicodeString(oemPassword, CP_OEMCP);
return S_OK;
}
STDMETHODIMP COpenArchiveCallback::CryptoGetTextPassword(BSTR *password)
{
if (!PasswordIsDefined)
{
RINOK(GetPassword(Password));
PasswordIsDefined = true;
}
return StringToBstr(Password, password);
}
/*
HRESULT OpenArchive(const CSysString &fileName,
IInFolderArchive **archiveHandlerResult,
CArchiverInfo &archiverInfoResult,
UString &defaultName,
IArchiveOpenCallback *openArchiveCallback)
{
HRESULT OpenArchive(const CSysString &fileName,
IInArchive **archive,
CArchiverInfo &archiverInfoResult,
IArchiveOpenCallback *openArchiveCallback);
}
*/
static HANDLE MyOpenFilePluginW(const wchar_t *name)
{
FString normalizedName = us2fs(name);
normalizedName.Trim();
FString fullName;
MyGetFullPathName(normalizedName, fullName);
NFind::CFileInfo fileInfo;
if (!fileInfo.Find(fullName))
return INVALID_HANDLE_VALUE;
if (fileInfo.IsDir())
return INVALID_HANDLE_VALUE;
CMyComPtr<IInFolderArchive> archiveHandler;
// CArchiverInfo archiverInfoResult;
// ::OutputDebugStringA("before OpenArchive\n");
CScreenRestorer screenRestorer;
{
screenRestorer.Save();
}
COpenArchiveCallback *openArchiveCallbackSpec = new COpenArchiveCallback;
CMyComPtr<IArchiveOpenCallback> openArchiveCallback = openArchiveCallbackSpec;
// if ((opMode & OPM_SILENT) == 0 && (opMode & OPM_FIND ) == 0)
openArchiveCallbackSpec->Init();
{
FString dirPrefix, fileName;
GetFullPathAndSplit(fullName, dirPrefix, fileName);
openArchiveCallbackSpec->LoadFileInfo(dirPrefix, fileName);
}
// ::OutputDebugStringA("before OpenArchive\n");
CAgent *agent = new CAgent;
archiveHandler = agent;
CMyComBSTR archiveType;
HRESULT result = archiveHandler->Open(NULL,
GetUnicodeString(fullName, CP_OEMCP), UString(), &archiveType, openArchiveCallback);
/*
HRESULT result = ::OpenArchive(fullName, &archiveHandler,
archiverInfoResult, defaultName, openArchiveCallback);
*/
if (result != S_OK)
{
if (result == E_ABORT)
return (HANDLE)-2;
ShowSysErrorMessage(result);
return INVALID_HANDLE_VALUE;
}
UString errorMessage = agent->GetErrorMessage();
if (!errorMessage.IsEmpty())
g_StartupInfo.ShowErrorMessage(UnicodeStringToMultiByte(errorMessage, CP_OEMCP));
// ::OutputDebugStringA("after OpenArchive\n");
CPlugin *plugin = new CPlugin(
fullName,
// defaultName,
agent,
(const wchar_t *)archiveType
);
if (!plugin)
return INVALID_HANDLE_VALUE;
plugin->PasswordIsDefined = openArchiveCallbackSpec->PasswordIsDefined;
plugin->Password = openArchiveCallbackSpec->Password;
// OutputDebugStringA("--- OpenFilePlugin ---- END");
return (HANDLE)(plugin);
}
static HANDLE MyOpenFilePlugin(const char *name)
{
UINT codePage =
#ifdef UNDER_CE
CP_OEMCP;
#else
::AreFileApisANSI() ? CP_ACP : CP_OEMCP;
#endif
return MyOpenFilePluginW(GetUnicodeString(name, codePage));
}
EXTERN_C HANDLE WINAPI OpenFilePlugin(char *name, const unsigned char * /* data */, int /* dataSize */)
{
MY_TRY_BEGIN;
// OutputDebugStringA("--- OpenFilePlugin");
if (name == NULL || (!g_Options.Enabled))
{
// if (!Opt.ProcessShiftF1)
return(INVALID_HANDLE_VALUE);
}
return MyOpenFilePlugin(name);
MY_TRY_END2("OpenFilePlugin", INVALID_HANDLE_VALUE);
}
/*
EXTERN_C HANDLE WINAPI OpenFilePluginW(const wchar_t *name,const unsigned char *Data,int DataSize,int OpMode)
{
MY_TRY_BEGIN;
if (name == NULL || (!g_Options.Enabled))
{
// if (!Opt.ProcessShiftF1)
return(INVALID_HANDLE_VALUE);
}
return MyOpenFilePluginW(name);
::OutputDebugStringA("OpenFilePluginW\n");
MY_TRY_END2("OpenFilePluginW", INVALID_HANDLE_VALUE);
}
*/
EXTERN_C HANDLE WINAPI OpenPlugin(int openFrom, INT_PTR item)
{
MY_TRY_BEGIN;
if(openFrom == OPEN_COMMANDLINE)
{
AString fileName = (const char *)item;
if(fileName.IsEmpty())
return INVALID_HANDLE_VALUE;
if (fileName.Len() >= 2 &&
fileName[0] == '\"' && fileName.Back() == '\"')
{
fileName.DeleteBack();
fileName.DeleteFrontal(1);
}
return MyOpenFilePlugin(fileName);
}
if(openFrom == OPEN_PLUGINSMENU)
{
switch(item)
{
case 0:
{
PluginPanelItem pluginPanelItem;
if(!g_StartupInfo.ControlGetActivePanelCurrentItemInfo(pluginPanelItem))
throw 142134;
return MyOpenFilePlugin(pluginPanelItem.FindData.cFileName);
}
case 1:
{
CObjectVector<PluginPanelItem> pluginPanelItem;
if (!g_StartupInfo.ControlGetActivePanelSelectedOrCurrentItems(pluginPanelItem))
throw 142134;
HRESULT res = CompressFiles(pluginPanelItem);
if (res != S_OK && res != E_ABORT)
{
ShowSysErrorMessage(res);
}
// if (res == S_OK)
{
/* int t = */ g_StartupInfo.ControlClearPanelSelection();
g_StartupInfo.ControlRequestActivePanel(FCTL_UPDATEPANEL, NULL);
g_StartupInfo.ControlRequestActivePanel(FCTL_REDRAWPANEL, NULL);
g_StartupInfo.ControlRequestActivePanel(FCTL_UPDATEANOTHERPANEL, NULL);
g_StartupInfo.ControlRequestActivePanel(FCTL_REDRAWANOTHERPANEL, NULL);
}
return INVALID_HANDLE_VALUE;
}
default:
throw 4282215;
}
}
return INVALID_HANDLE_VALUE;
MY_TRY_END2("OpenPlugin", INVALID_HANDLE_VALUE);
}
EXTERN_C void WINAPI ClosePlugin(HANDLE plugin)
{
// OutputDebugStringA("-- ClosePlugin --- START");
MY_TRY_BEGIN;
delete (CPlugin *)plugin;
// OutputDebugStringA("-- ClosePlugin --- END");
MY_TRY_END1("ClosePlugin");
}
EXTERN_C int WINAPI GetFindData(HANDLE plugin, struct PluginPanelItem **panelItems, int *itemsNumber, int opMode)
{
MY_TRY_BEGIN;
return(((CPlugin *)plugin)->GetFindData(panelItems, itemsNumber, opMode));
MY_TRY_END2("GetFindData", FALSE);
}
EXTERN_C void WINAPI FreeFindData(HANDLE plugin, struct PluginPanelItem *panelItems, int itemsNumber)
{
MY_TRY_BEGIN;
((CPlugin *)plugin)->FreeFindData(panelItems, itemsNumber);
MY_TRY_END1("FreeFindData");
}
EXTERN_C int WINAPI GetFiles(HANDLE plugin, struct PluginPanelItem *panelItems,
int itemsNumber, int move, char *destPath, int opMode)
{
MY_TRY_BEGIN;
return(((CPlugin *)plugin)->GetFiles(panelItems, itemsNumber, move, destPath, opMode));
MY_TRY_END2("GetFiles", NFileOperationReturnCode::kError);
}
EXTERN_C int WINAPI SetDirectory(HANDLE plugin, const char *dir, int opMode)
{
MY_TRY_BEGIN;
return(((CPlugin *)plugin)->SetDirectory(dir, opMode));
MY_TRY_END2("SetDirectory", FALSE);
}
EXTERN_C void WINAPI GetPluginInfo(struct PluginInfo *info)
{
MY_TRY_BEGIN;
info->StructSize = sizeof(*info);
info->Flags = 0;
info->DiskMenuStrings = NULL;
info->DiskMenuNumbers = NULL;
info->DiskMenuStringsNumber = 0;
static const char *pluginMenuStrings[2];
pluginMenuStrings[0] = g_StartupInfo.GetMsgString(NMessageID::kOpenArchiveMenuString);
pluginMenuStrings[1] = g_StartupInfo.GetMsgString(NMessageID::kCreateArchiveMenuString);
info->PluginMenuStrings = (char **)pluginMenuStrings;
info->PluginMenuStringsNumber = 2;
static const char *pluginCfgStrings[1];
pluginCfgStrings[0] = g_StartupInfo.GetMsgString(NMessageID::kOpenArchiveMenuString);
info->PluginConfigStrings = (char **)pluginCfgStrings;
info->PluginConfigStringsNumber = ARRAY_SIZE(pluginCfgStrings);
info->CommandPrefix = (char *)kCommandPrefix;
MY_TRY_END1("GetPluginInfo");
}
EXTERN_C int WINAPI Configure(int /* itemNumber */)
{
MY_TRY_BEGIN;
const int kEnabledCheckBoxIndex = 1;
const int kYSize = 7;
struct CInitDialogItem initItems[]=
{
{ DI_DOUBLEBOX, 3, 1, 72, kYSize - 2, false, false, 0, false, NMessageID::kConfigTitle, NULL, NULL },
{ DI_CHECKBOX, 5, 2, 0, 0, true, g_Options.Enabled, 0, false, NMessageID::kConfigPluginEnabled, NULL, NULL },
{ DI_TEXT, 5, 3, 0, 0, false, false, DIF_BOXCOLOR | DIF_SEPARATOR, false, -1, "", NULL },
{ DI_BUTTON, 0, kYSize - 3, 0, 0, false, false, DIF_CENTERGROUP, true, NMessageID::kOk, NULL, NULL },
{ DI_BUTTON, 0, kYSize - 3, 0, 0, false, false, DIF_CENTERGROUP, false, NMessageID::kCancel, NULL, NULL },
};
const int kNumDialogItems = ARRAY_SIZE(initItems);
const int kOkButtonIndex = kNumDialogItems - 2;
FarDialogItem dialogItems[kNumDialogItems];
g_StartupInfo.InitDialogItems(initItems, dialogItems, kNumDialogItems);
int askCode = g_StartupInfo.ShowDialog(76, kYSize,
kHelpTopicConfig, dialogItems, kNumDialogItems);
if (askCode != kOkButtonIndex)
return (FALSE);
g_Options.Enabled = BOOLToBool(dialogItems[kEnabledCheckBoxIndex].Selected);
g_StartupInfo.SetRegKeyValue(HKEY_CURRENT_USER, kRegisrtryMainKeyName,
kRegisrtryValueNameEnabled, g_Options.Enabled);
return(TRUE);
MY_TRY_END2("Configure", FALSE);
}
EXTERN_C void WINAPI GetOpenPluginInfo(HANDLE plugin,struct OpenPluginInfo *info)
{
MY_TRY_BEGIN;
((CPlugin *)plugin)->GetOpenPluginInfo(info);
MY_TRY_END1("GetOpenPluginInfo");
}
EXTERN_C int WINAPI PutFiles(HANDLE plugin, struct PluginPanelItem *panelItems, int itemsNumber, int move, int opMode)
{
MY_TRY_BEGIN;
return (((CPlugin *)plugin)->PutFiles(panelItems, itemsNumber, move, opMode));
MY_TRY_END2("PutFiles", NFileOperationReturnCode::kError);
}
EXTERN_C int WINAPI DeleteFiles(HANDLE plugin, PluginPanelItem *panelItems, int itemsNumber, int opMode)
{
MY_TRY_BEGIN;
return (((CPlugin *)plugin)->DeleteFiles(panelItems, itemsNumber, opMode));
MY_TRY_END2("DeleteFiles", FALSE);
}
EXTERN_C int WINAPI ProcessKey(HANDLE plugin, int key, unsigned int controlState)
{
MY_TRY_BEGIN;
return (((CPlugin *)plugin)->ProcessKey(key, controlState));
MY_TRY_END2("ProcessKey", FALSE);
}
|