| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- //---------------------------------------------------------------------------
- #define NO_WIN32_LEAN_AND_MEAN
- #include <vcl.h>
- #pragma hdrstop
- #include <Consts.hpp>
- #include <shlobj.h>
- #include <Common.h>
- #include "GUITools.h"
- #include "GUIConfiguration.h"
- #include <TextsWin.h>
- #include <TextsCore.h>
- #include <CoreMain.h>
- #include <SessionData.h>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- bool __fastcall FindFile(AnsiString & Path)
- {
- bool Result = FileExists(Path);
- if (!Result)
- {
- int Len = GetEnvironmentVariable("PATH", NULL, 0);
- if (Len > 0)
- {
- AnsiString Paths;
- Paths.SetLength(Len - 1);
- GetEnvironmentVariable("PATH", Paths.c_str(), Len);
- AnsiString NewPath = FileSearch(ExtractFileName(Path), Paths);
- Result = !NewPath.IsEmpty();
- if (Result)
- {
- Path = NewPath;
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall FileExistsEx(AnsiString Path)
- {
- return FindFile(Path);
- }
- //---------------------------------------------------------------------------
- void __fastcall OpenSessionInPutty(const AnsiString PuttyPath,
- TSessionData * SessionData, const AnsiString Password)
- {
- AnsiString Program, Params, Dir;
- SplitCommand(PuttyPath, Program, Params, Dir);
- Program = ExpandEnvironmentVariables(Program);
- if (FindFile(Program))
- {
- AnsiString SessionName;
- TRegistryStorage * Storage = NULL;
- TSessionData * ExportData = NULL;
- TRegistryStorage * SourceStorage = NULL;
- try
- {
- Storage = new TRegistryStorage(Configuration->PuttySessionsKey);
- Storage->AccessMode = smReadWrite;
- if (Storage->OpenRootKey(true))
- {
- if (Storage->KeyExists(SessionData->StorageKey))
- {
- SessionName = SessionData->SessionName;
- }
- else
- {
- SourceStorage = new TRegistryStorage(Configuration->PuttySessionsKey);
- if (SourceStorage->OpenSubKey(MungeStr(StoredSessions->DefaultSettings->Name), false) &&
- Storage->OpenSubKey(MungeStr(GUIConfiguration->PuttySession), true))
- {
- Storage->Copy(SourceStorage);
- Storage->CloseSubKey();
- }
- ExportData = new TSessionData("");
- ExportData->Assign(SessionData);
- ExportData->Modified = true;
- ExportData->Name = GUIConfiguration->PuttySession;
- ExportData->Password = "";
- ExportData->Save(Storage, true);
- SessionName = GUIConfiguration->PuttySession;
- }
- }
- }
- __finally
- {
- delete Storage;
- delete ExportData;
- delete SourceStorage;
- }
- if (!Params.IsEmpty())
- {
- Params += " ";
- }
- if (!Password.IsEmpty())
- {
- Params += FORMAT("-pw \"%s\" ", (Password));
- }
- Params += FORMAT("-load \"%s\"", (SessionName));
- if (!ExecuteShell(Program, Params))
- {
- throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
- }
- }
- else
- {
- throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall ExecuteShell(const AnsiString Path, const AnsiString Params)
- {
- return ((int)ShellExecute(NULL, "open", (char*)Path.data(),
- (char*)Params.data(), NULL, SW_SHOWNORMAL) > 32);
- }
- //---------------------------------------------------------------------------
- bool __fastcall ExecuteShell(const AnsiString Path, const AnsiString Params,
- HANDLE & Handle)
- {
- bool Result;
- TShellExecuteInfo ExecuteInfo;
- memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
- ExecuteInfo.cbSize = sizeof(ExecuteInfo);
- ExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
- ExecuteInfo.hwnd = Application->Handle;
- ExecuteInfo.lpFile = (char*)Path.data();
- ExecuteInfo.lpParameters = (char*)Params.data();
- ExecuteInfo.nShow = SW_SHOW;
- Result = (ShellExecuteEx(&ExecuteInfo) != 0);
- if (Result)
- {
- Handle = ExecuteInfo.hProcess;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall ExecuteShellAndWait(HWND Handle, const AnsiString Path,
- const AnsiString Params, TProcessMessagesEvent ProcessMessages)
- {
- bool Result;
- TShellExecuteInfo ExecuteInfo;
- memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
- ExecuteInfo.cbSize = sizeof(ExecuteInfo);
- ExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
- ExecuteInfo.hwnd = Handle;
- ExecuteInfo.lpFile = (char*)Path.data();
- ExecuteInfo.lpParameters = (char*)Params.data();
- ExecuteInfo.nShow = SW_SHOW;
- Result = (ShellExecuteEx(&ExecuteInfo) != 0);
- if (Result)
- {
- if (ProcessMessages != NULL)
- {
- unsigned long WaitResult;
- do
- {
- WaitResult = WaitForSingleObject(ExecuteInfo.hProcess, 200);
- if (WaitResult == WAIT_FAILED)
- {
- throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
- }
- ProcessMessages();
- }
- while (WaitResult == WAIT_TIMEOUT);
- }
- else
- {
- WaitForSingleObject(ExecuteInfo.hProcess, INFINITE);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall ExecuteShellAndWait(HWND Handle, const AnsiString Command,
- TProcessMessagesEvent ProcessMessages)
- {
- AnsiString Program, Params, Dir;
- SplitCommand(Command, Program, Params, Dir);
- return ExecuteShellAndWait(Handle, Program, Params, ProcessMessages);
- }
- //---------------------------------------------------------------------------
- bool __fastcall SpecialFolderLocation(int PathID, AnsiString & Path)
- {
- LPITEMIDLIST Pidl;
- char Buf[256];
- if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
- SHGetPathFromIDList(Pidl, Buf))
- {
- Path = AnsiString(Buf);
- return true;
- }
- return false;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall ItemsFormatString(const AnsiString SingleItemFormat,
- const AnsiString MultiItemsFormat, int Count, const AnsiString FirstItem)
- {
- AnsiString Result;
- if (Count == 1)
- {
- Result = FORMAT(SingleItemFormat, (FirstItem));
- }
- else
- {
- Result = FORMAT(MultiItemsFormat, (Count));
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall ItemsFormatString(const AnsiString SingleItemFormat,
- const AnsiString MultiItemsFormat, TStrings * Items)
- {
- return ItemsFormatString(SingleItemFormat, MultiItemsFormat,
- Items->Count, (Items->Count > 0 ? Items->Strings[0] : AnsiString()));
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall FileNameFormatString(const AnsiString SingleFileFormat,
- const AnsiString MultiFilesFormat, TStrings * Files, bool Remote)
- {
- assert(Files != NULL);
- AnsiString Item;
- if (Files->Count > 0)
- {
- Item = Remote ? UnixExtractFileName(Files->Strings[0]) :
- ExtractFileName(Files->Strings[0]);
- }
- return ItemsFormatString(SingleFileFormat, MultiFilesFormat,
- Files->Count, Item);
- }
- //---------------------------------------------------------------------
- AnsiString __fastcall FormatBytes(__int64 Bytes, bool UseOrders)
- {
- AnsiString Result;
- if (!UseOrders || (Bytes < __int64(100*1024)))
- {
- Result = FormatFloat("#,##0 \"B\"", Bytes);
- }
- else if (Bytes < __int64(100*1024*1024))
- {
- Result = FormatFloat("#,##0 \"KB\"", Bytes / 1024);
- }
- else
- {
- Result = FormatFloat("#,##0 \"MB\"", Bytes / (1024*1024));
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall CopyToClipboard(AnsiString Text)
- {
- HANDLE Data;
- void * DataPtr;
- if (OpenClipboard(0))
- {
- try
- {
- Data = GlobalAlloc(GMEM_MOVEABLE + GMEM_DDESHARE, Text.Length() + 1);
- try
- {
- DataPtr = GlobalLock(Data);
- try
- {
- memcpy(DataPtr, Text.c_str(), Text.Length() + 1);
- EmptyClipboard();
- SetClipboardData(CF_TEXT, Data);
- }
- __finally
- {
- GlobalUnlock(Data);
- }
- }
- catch(...)
- {
- GlobalFree(Data);
- throw;
- }
- }
- __finally
- {
- CloseClipboard();
- }
- }
- else
- {
- throw Exception(Consts_SCannotOpenClipboard);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall CopyToClipboard(TStrings * Strings)
- {
- if (Strings->Count > 0)
- {
- if (Strings->Count == 1)
- {
- CopyToClipboard(Strings->Strings[0]);
- }
- else
- {
- CopyToClipboard(Strings->Text);
- }
- }
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall UniqTempDir(const AnsiString BaseDir, const AnsiString Identity,
- bool Mask)
- {
- AnsiString TempDir;
- do
- {
- TempDir = BaseDir.IsEmpty() ? SystemTemporaryDirectory() : BaseDir;
- TempDir = IncludeTrailingBackslash(TempDir) + Identity;
- if (Mask)
- {
- TempDir += "?????";
- }
- else
- {
- TempDir += IncludeTrailingBackslash(FormatDateTime("nnzzz", Now()));
- };
- }
- while (!Mask && DirectoryExists(TempDir));
- return TempDir;
- }
- //---------------------------------------------------------------------------
- bool __fastcall DeleteDirectory(const AnsiString DirName)
- {
- TSearchRec sr;
- bool retval = true;
- if (FindFirst(DirName + "\\*", faAnyFile, sr) == 0) // VCL Function
- {
- if (FLAGSET(sr.Attr, faDirectory))
- {
- if (sr.Name != "." && sr.Name != "..")
- retval = DeleteDirectory(DirName + "\\" + sr.Name);
- }
- else
- {
- retval = DeleteFile(DirName + "\\" + sr.Name);
- }
- if (retval)
- {
- while (FindNext(sr) == 0)
- { // VCL Function
- if (FLAGSET(sr.Attr, faDirectory))
- {
- if (sr.Name != "." && sr.Name != "..")
- retval = DeleteDirectory(DirName + "\\" + sr.Name);
- }
- else
- {
- retval = DeleteFile(DirName + "\\" + sr.Name);
- }
- if (!retval) break;
- }
- }
- }
- FindClose(sr);
- if (retval) retval = RemoveDir(DirName); // VCL function
- return retval;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TranslateExceptionMessage(const Exception * E)
- {
- if (dynamic_cast<const EAccessViolation*>(E) != NULL)
- {
- return LoadStr(ACCESS_VIOLATION_ERROR);
- }
- else
- {
- return E->Message;
- }
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall FormatDateTimeSpan(const AnsiString TimeFormat, TDateTime DateTime)
- {
- AnsiString Result;
- if (int(DateTime) > 0)
- {
- Result = IntToStr(int(DateTime)) + ", ";
- }
- // days are decremented, because when there is to many of them,
- // "integer overflow" error occures
- Result += FormatDateTime(TimeFormat, DateTime - int(DateTime));
- return Result;
- }
- //---------------------------------------------------------------------------
- TLocalCustomCommand::TLocalCustomCommand()
- {
- }
- //---------------------------------------------------------------------------
- TLocalCustomCommand::TLocalCustomCommand(const AnsiString & FileName,
- const AnsiString & LocalFileName, const AnsiString & FileList) :
- TFileCustomCommand(FileName, FileList)
- {
- FLocalFileName = LocalFileName;
- }
- //---------------------------------------------------------------------------
- int __fastcall TLocalCustomCommand::PatternLen(int Index, char PatternCmd)
- {
- int Len;
- if (PatternCmd == '^')
- {
- Len = 3;
- }
- else
- {
- Len = TFileCustomCommand::PatternLen(Index, PatternCmd);
- }
- return Len;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TLocalCustomCommand::PatternReplacement(int Index,
- const AnsiString & Pattern, AnsiString & Replacement, bool & Delimit)
- {
- bool Result;
- if (Pattern == "!^!")
- {
- Replacement = FLocalFileName;
- Result = true;
- }
- else
- {
- Result = TFileCustomCommand::PatternReplacement(Index, Pattern, Replacement, Delimit);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TLocalCustomCommand::DelimitReplacement(
- AnsiString & /*Replacement*/, char /*Quote*/)
- {
- // never delimit local commands
- }
- //---------------------------------------------------------------------------
- bool __fastcall TLocalCustomCommand::HasLocalFileName(const AnsiString & Command)
- {
- return FindPattern(Command, '^');
- }
- //---------------------------------------------------------------------------
- bool __fastcall TLocalCustomCommand::IsFileCommand(const AnsiString & Command)
- {
- return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
- }
|