DriveView.pas 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856
  1. unit DriveView;
  2. {==================================================================
  3. Component TDriveView / Version 2.6, January 2000
  4. ==================================================================
  5. Description:
  6. ============
  7. Displays the the directory structure of all drives as treeview
  8. with shell icons. Complete drag&Drop support for files and
  9. directories.
  10. Author:
  11. =======
  12. (c) Ingo Eckel 1998, 1999
  13. Sodener Weg 38
  14. 65812 Bad Soden
  15. Germany
  16. Modifications (for WinSCP):
  17. ===========================
  18. (c) Martin Prikryl 2004
  19. V2.6:
  20. - Shows "shared"-symbol with directories
  21. - Delphi5 compatible
  22. For detailed documentation and history see TDriveView.htm.
  23. {==================================================================}
  24. interface
  25. { Define ENHVALIDATE to scan all existing directories on a detected filesystem change:}
  26. {.$DEFINE ENHVALIDATE}
  27. {Required compiler options for TDriveView:}
  28. {$A+,B-,X+,H+,P+}
  29. {$WARN SYMBOL_PLATFORM OFF}
  30. uses
  31. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ComObj,
  32. Dialogs, ComCtrls, ShellApi, CommCtrl, ExtCtrls, ActiveX, ShlObj,
  33. DirView, ShellDialogs, DragDrop, DragDropFilesEx, FileChanges, FileOperator,
  34. DiscMon, IEDriveInfo, IEListView, PIDL, BaseUtils, CustomDirView,
  35. CustomDriveView, System.Generics.Collections;
  36. const
  37. msThreadChangeDelay = 50;
  38. ErrorNodeNA = '%s: Node not assigned';
  39. {Flags used by TDriveView.RefreshRootNodes:}
  40. dvdsFloppy = 8; {Include floppy drives}
  41. dvdsRereadAllways = 16; {Refresh drivestatus in any case}
  42. WM_USER_SHCHANGENOTIFY = WM_USER + $2000 + 13;
  43. type
  44. EInvalidDirName = class(Exception);
  45. ENodeNotAssigned = class(Exception);
  46. TDriveStatus = class
  47. Scanned: Boolean; {Drive allready scanned?}
  48. Verified: Boolean; {Drive completly scanned?}
  49. RootNode: TTreeNode; {Rootnode to drive}
  50. RootNodeIndex: Integer;
  51. DiscMonitor: TDiscMonitor; {Monitor thread}
  52. ChangeTimer: TTimer; {Change timer for the monitor thread}
  53. DefaultDir: string; {Current directory}
  54. DriveHandle: THandle;
  55. NotificationHandle: HDEVNOTIFY;
  56. end;
  57. TDriveStatusPair = TPair<string, TDriveStatus>;
  58. TScanDirInfo = record
  59. SearchNewDirs: Boolean;
  60. StartNode: TTreeNode;
  61. DriveType: Integer;
  62. end;
  63. PScanDirInfo = ^TScanDirInfo;
  64. TDriveView = class;
  65. TNodeData = class
  66. private
  67. FDirName: string;
  68. FShortName: string;
  69. FAttr: Integer;
  70. FScanned: Boolean;
  71. FData: Pointer;
  72. FIsRecycleBin: Boolean;
  73. FIconEmpty: Boolean;
  74. public
  75. shAttr: ULONG;
  76. PIDL: PItemIDList;
  77. ShellFolder: IShellFolder;
  78. constructor Create;
  79. destructor Destroy; override;
  80. property DirName: string read FDirName write FDirName;
  81. property ShortName: string read FShortName write FShortName;
  82. property Attr: Integer read FAttr write FAttr;
  83. property Scanned: Boolean read FScanned write FScanned;
  84. property Data: Pointer read FData write FData;
  85. property IsRecycleBin: Boolean read FIsRecycleBin;
  86. property IconEmpty: Boolean read FIconEmpty write FIconEmpty;
  87. end;
  88. TDriveTreeNode = class(TTreeNode)
  89. procedure Assign(Source: TPersistent); override;
  90. end;
  91. TDriveView = class(TCustomDriveView)
  92. private
  93. FDriveStatus: TObjectDictionary<string, TDriveStatus>;
  94. FConfirmDelete: Boolean;
  95. FConfirmOverwrite: Boolean;
  96. FWatchDirectory: Boolean;
  97. FDirectory: string;
  98. FFullDriveScan: Boolean;
  99. FShowVolLabel: Boolean;
  100. FVolDisplayStyle: TVolumeDisplayStyle;
  101. FChangeFlag: Boolean;
  102. FLastDir: string;
  103. FValidateFlag: Boolean;
  104. FCreating: Boolean;
  105. FForceRename: Boolean;
  106. FRenameNode: TTreeNode;
  107. FLastRenameName: string;
  108. FInternalWindowHandle: HWND;
  109. FChangeNotify: ULONG;
  110. FPrevSelected: TTreeNode;
  111. FPrevSelectedIndex: Integer;
  112. FChangeTimerSuspended: Integer;
  113. FDesktop: IShellFolder;
  114. {Additional events:}
  115. FOnDisplayContextMenu: TNotifyEvent;
  116. FOnRefreshDrives: TNotifyEvent;
  117. FOnNeedHiddenDirectories: TNotifyEvent;
  118. {used components:}
  119. FDirView: TDirView;
  120. FFileOperator: TFileOperator;
  121. FChangeInterval: Cardinal;
  122. {Drag&drop:}
  123. FLastPathCut: string;
  124. {Drag&drop helper functions:}
  125. procedure SignalDirDelete(Sender: TObject; Files: TStringList);
  126. function CheckForSubDirs(Path: string): Boolean;
  127. function ReadSubDirs(Node: TTreeNode; DriveType: Integer): Boolean;
  128. {Callback-functions used by iteratesubtree:}
  129. function CallBackValidateDir(var Node: TTreeNode; Data: Pointer): Boolean;
  130. { Notification procedures used by component TDiscMonitor: }
  131. procedure ChangeDetected(Sender: TObject; const Directory: string;
  132. var SubdirsChanged: Boolean);
  133. procedure ChangeInvalid(Sender: TObject; const Directory: string; const ErrorStr: string);
  134. {Notification procedure used by component TTimer:}
  135. procedure ChangeTimerOnTimer(Sender: TObject);
  136. protected
  137. procedure SetSelected(Node: TTreeNode);
  138. procedure SetFullDriveScan(DoFullDriveScan: Boolean);
  139. procedure SetWatchDirectory(Value: Boolean);
  140. procedure SetShowVolLabel(ShowIt: Boolean);
  141. procedure SetDirView(Value: TDirView);
  142. procedure SetDirectory(Value: string); override;
  143. procedure GetNodeShellAttr(ParentNode: TTreeNode; NodeData: TNodeData; GetAttr: Boolean);
  144. function DoScanDir(FromNode: TTreeNode): Boolean;
  145. function AddChildNode(ParentNode: TTreeNode; SRec: TSearchRec): TTreeNode;
  146. procedure CreateWatchThread(Drive: string);
  147. function NodeWatched(Node: TTreeNode): Boolean;
  148. procedure TerminateWatchThread(Drive: string);
  149. function WatchThreadActive: Boolean; overload;
  150. function WatchThreadActive(Drive: string): Boolean; overload;
  151. procedure InternalWndProc(var Msg: TMessage);
  152. procedure UpdateDriveNotifications(Drive: string);
  153. procedure DriveRemoved(Drive: string);
  154. procedure DriveRemoving(Drive: string);
  155. procedure CancelDriveRefresh;
  156. procedure ScheduleDriveRefresh;
  157. function DirAttrMask: Integer;
  158. function CreateDriveStatus: TDriveStatus;
  159. procedure ValidateDirectoryEx(Node: TTreeNode; Recurse: TRecursiveScan;
  160. NewDirs: Boolean); override;
  161. procedure RebuildTree; override;
  162. procedure SetLastPathCut(Path: string);
  163. function GetCanUndoCopyMove: Boolean; virtual;
  164. procedure CreateWnd; override;
  165. procedure DestroyWnd; override;
  166. procedure Edit(const Item: TTVItem); override;
  167. procedure WMUserRename(var Message: TMessage); message WM_USER_RENAME;
  168. function GetCustomDirView: TCustomDirView; override;
  169. procedure SetCustomDirView(Value: TCustomDirView); override;
  170. function NodePath(Node: TTreeNode): string; override;
  171. function NodeIsRecycleBin(Node: TTreeNode): Boolean; override;
  172. function NodePathExists(Node: TTreeNode): Boolean; override;
  173. function NodeColor(Node: TTreeNode): TColor; override;
  174. function FindPathNode(Path: string): TTreeNode; override;
  175. function DoFindNodeToPath(Path: string; ExistingOnly: Boolean): TTreeNode;
  176. function CreateNode: TTreeNode; override;
  177. function DDSourceEffects: TDropEffectSet; override;
  178. procedure DDChooseEffect(KeyState: Integer; var Effect: Integer; PreferredEffect: Integer); override;
  179. function DragCompleteFileList: Boolean; override;
  180. function DDExecute: TDragResult; override;
  181. public
  182. property Images;
  183. property StateImages;
  184. property Items stored False;
  185. property Selected Write SetSelected stored False;
  186. property DragImageList: TDragImageList read FDragImageList;
  187. property CanUndoCopyMove: Boolean read GetCanUndoCopyMove;
  188. property DDFileOperator: TFileOperator read FFileOperator;
  189. property LastPathCut: string read FLastPathCut write SetLastPathCut;
  190. function UndoCopyMove: Boolean; dynamic;
  191. procedure EmptyClipboard; dynamic;
  192. function CopyToClipBoard(Node: TTreeNode): Boolean; dynamic;
  193. function CutToClipBoard(Node: TTreeNode): Boolean; dynamic;
  194. function CanPasteFromClipBoard: Boolean; dynamic;
  195. function PasteFromClipBoard(TargetPath: string = ''): Boolean; dynamic;
  196. procedure PerformDragDropFileOperation(Node: TTreeNode; Effect: Integer); override;
  197. {Drive handling:}
  198. function GetDriveStatus(Drive: string): TDriveStatus;
  199. function GetDriveTypetoNode(Node: TTreeNode): Integer; {Returns DRIVE_CDROM etc..}
  200. function GetDriveToNode(Node: TTreeNode): string;
  201. function GetDriveText(Drive: string): string;
  202. procedure ScanDrive(Drive: string);
  203. procedure RefreshRootNodes(dsFlags: Integer);
  204. function GetDrives: TStrings;
  205. {Node handling:}
  206. procedure SetImageIndex(Node: TTreeNode); virtual;
  207. function FindNodeToPath(Path: string): TTreeNode;
  208. function TryFindNodeToPath(Path: string): TTreeNode;
  209. function RootNode(Node: TTreeNode): TTreeNode;
  210. function GetDirName(Node: TTreeNode): string;
  211. function GetDisplayName(Node: TTreeNode): string;
  212. function NodePathName(Node: TTreeNode): string; override;
  213. function GetFQPIDL(Node: TTreeNode): PItemIDList;
  214. constructor Create(AOwner: TComponent); override;
  215. destructor Destroy; override;
  216. {Menu-handling:}
  217. procedure DisplayContextMenu(Node: TTreeNode; Point: TPoint); override;
  218. procedure DisplayPropertiesMenu(Node: TTreeNode); override;
  219. {Watchthread handling:}
  220. procedure StartWatchThread;
  221. procedure StopWatchThread;
  222. procedure SuspendChangeTimer;
  223. procedure ResumeChangeTimer;
  224. procedure StartAllWatchThreads;
  225. procedure StopAllWatchThreads;
  226. procedure ValidateCurrentDirectoryIfNotMonitoring;
  227. (* Modified Events: *)
  228. procedure GetImageIndex(Node: TTreeNode); override;
  229. function CanEdit(Node: TTreeNode): Boolean; override;
  230. function CanChange(Node: TTreeNode): Boolean; override;
  231. function CanExpand(Node: TTreeNode): Boolean; override;
  232. procedure Delete(Node: TTreeNode); override;
  233. procedure Loaded; override;
  234. procedure KeyPress(var Key: Char); override;
  235. procedure Change(Node: TTreeNode); override;
  236. published
  237. {Additional properties:}
  238. {Current selected directory:}
  239. property Directory;
  240. {Confirm deleting directories:}
  241. property ConfirmDelete: Boolean read FConfirmDelete write FConfirmDelete default True;
  242. {Confirm overwriting directories:}
  243. property ConfirmOverwrite: Boolean read FConfirmOverwrite write FConfirmOverwrite default True;
  244. {Scan all directories in method ScanDrive:}
  245. property FullDriveScan: Boolean read FFullDriveScan write SetFullDriveScan default False;
  246. {Enable automatic update on filesystem changes:}
  247. property WatchDirectory: Boolean read FWatchDirectory write SetWatchDirectory default False;
  248. {Linked component TDirView:}
  249. property DirView: TDirView read FDirView write SetDirView;
  250. {Show the volume labels of drives:}
  251. property ShowVolLabel: Boolean read FShowVolLabel write SetShowVolLabel default True;
  252. {Additional events:}
  253. property OnDisplayContextMenu: TNotifyEvent read FOnDisplayContextMenu
  254. write FOnDisplayContextMenu;
  255. property OnRefreshDrives: TNotifyEvent read FOnRefreshDrives
  256. write FOnRefreshDrives;
  257. property OnBusy;
  258. property DDLinkOnExeDrag;
  259. property TargetPopUpMenu;
  260. property OnDDDragEnter;
  261. property OnDDDragLeave;
  262. property OnDDDragOver;
  263. property OnDDDrop;
  264. property OnDDQueryContinueDrag;
  265. property OnDDGiveFeedback;
  266. property OnDDDragDetect;
  267. property OnDDProcessDropped;
  268. property OnDDError;
  269. property OnDDExecuted;
  270. property OnDDFileOperation;
  271. property OnDDFileOperationExecuted;
  272. property Align;
  273. property Anchors;
  274. property AutoExpand;
  275. property BiDiMode;
  276. property BorderStyle;
  277. property BorderWidth;
  278. property ChangeDelay;
  279. property Color;
  280. property Ctl3D;
  281. property Constraints;
  282. property DoubleBuffered;
  283. {Delphi's drag&drop is not compatible with the OLE windows drag&drop:}
  284. property DragKind;
  285. property DragCursor;
  286. property DragMode Default dmAutomatic;
  287. property OnDragDrop;
  288. property OnDragOver;
  289. property Enabled;
  290. property Font;
  291. property HideSelection;
  292. property HotTrack;
  293. property Indent;
  294. property ParentBiDiMode;
  295. property ParentColor;
  296. property ParentCtl3D;
  297. property ParentDoubleBuffered;
  298. property ParentFont;
  299. property ParentShowHint;
  300. property PopupMenu;
  301. property ReadOnly;
  302. property RightClickSelect;
  303. property RowSelect;
  304. property ShowButtons;
  305. property ShowHint;
  306. property ShowLines;
  307. property TabOrder;
  308. property TabStop default True;
  309. property ToolTips;
  310. property Visible;
  311. property OnChange;
  312. property OnChanging;
  313. property OnClick;
  314. property OnCollapsing;
  315. property OnCollapsed;
  316. property OnCompare;
  317. property OnDblClick;
  318. property OnDeletion;
  319. property OnEdited;
  320. property OnEditing;
  321. property OnEndDock;
  322. property OnEndDrag;
  323. property OnEnter;
  324. property OnExit;
  325. property OnExpanding;
  326. property OnExpanded;
  327. property OnGetImageIndex;
  328. property OnGetSelectedIndex;
  329. property OnKeyDown;
  330. property OnKeyPress;
  331. property OnKeyUp;
  332. property OnMouseDown;
  333. property OnMouseMove;
  334. property OnMouseUp;
  335. property OnStartDock;
  336. property OnStartDrag;
  337. property OnNeedHiddenDirectories: TNotifyEvent read FOnNeedHiddenDirectories write FOnNeedHiddenDirectories;
  338. end;
  339. procedure Register;
  340. implementation
  341. uses
  342. CompThread, PasTools, UITypes, Types, OperationWithTimeout, System.Generics.Defaults;
  343. type
  344. PInt = ^Integer;
  345. procedure Register;
  346. begin
  347. RegisterComponents('DriveDir', [TDriveView]);
  348. end; {Register}
  349. constructor TNodeData.Create;
  350. begin
  351. inherited;
  352. FAttr := 0;
  353. FScanned := False;
  354. FDirName := '';
  355. FShortName := '';
  356. FIsRecycleBin := False;
  357. FIconEmpty := True;
  358. shAttr := 0;
  359. PIDL := nil;
  360. ShellFolder := nil;
  361. end; {TNodeData.Create}
  362. destructor TNodeData.Destroy;
  363. begin
  364. SetLength(FDirName, 0);
  365. if Assigned(PIDL) then
  366. FreePIDL(PIDL);
  367. inherited;
  368. end; {TNodeData.Destroy}
  369. { TDriveTreeNode }
  370. procedure TDriveTreeNode.Assign(Source: TPersistent);
  371. var
  372. SourceData: TNodeData;
  373. NewData: TNodeData;
  374. begin
  375. inherited Assign(Source);
  376. if not Deleting and (Source is TTreeNode) then
  377. begin
  378. SourceData := TNodeData(TTreeNode(Source).Data);
  379. NewData := TNodeData.Create();
  380. NewData.DirName := SourceData.DirName;
  381. NewData.ShortName := SourceData.ShortName;
  382. NewData.Attr := SourceData.Attr;
  383. NewData.Scanned := SourceData.Scanned;
  384. NewData.Data := SourceData.Data;
  385. NewData.FIsRecycleBin := SourceData.FIsRecycleBin;
  386. NewData.IconEmpty := SourceData.IconEmpty;
  387. TTreeNode(Source).Data := NewData;
  388. end;
  389. end;
  390. { TDriveView }
  391. constructor TDriveView.Create(AOwner: TComponent);
  392. var
  393. Drive: TRealDrive;
  394. ChangeNotifyEntry: TSHChangeNotifyEntry;
  395. Dummy: string;
  396. begin
  397. inherited;
  398. FCreating := True;
  399. FDriveStatus := TObjectDictionary<string, TDriveStatus>.Create([doOwnsValues]);
  400. FChangeInterval := MSecsPerSec;
  401. for Drive := FirstDrive to LastDrive do
  402. begin
  403. FDriveStatus.Add(Drive, CreateDriveStatus);
  404. end;
  405. FFileOperator := TFileOperator.Create(Self);
  406. FShowVolLabel := True;
  407. FChangeFlag := False;
  408. FLastDir := EmptyStr;
  409. FValidateFlag := False;
  410. FConfirmDelete := True;
  411. FDirectory := EmptyStr;
  412. FForceRename := False;
  413. FLastRenameName := '';
  414. FRenameNode := nil;
  415. FPrevSelected := nil;
  416. FPrevSelectedIndex := -1;
  417. FChangeTimerSuspended := 0;
  418. FConfirmOverwrite := True;
  419. FLastPathCut := '';
  420. FStartPos.X := -1;
  421. FStartPos.Y := -1;
  422. FDragPos := FStartPos;
  423. FInternalWindowHandle := Classes.AllocateHWnd(InternalWndProc);
  424. // Source: petr.solin 2022-02-25
  425. FChangeNotify := 0;
  426. if SpecialFolderLocation(CSIDL_DESKTOP, Dummy, ChangeNotifyEntry.pidl) then
  427. begin
  428. ChangeNotifyEntry.fRecursive := False;
  429. FChangeNotify :=
  430. SHChangeNotifyRegister(
  431. FInternalWindowHandle, SHCNRF_ShellLevel or SHCNRF_NewDelivery,
  432. SHCNE_RENAMEFOLDER or SHCNE_MEDIAINSERTED or SHCNE_MEDIAREMOVED,
  433. WM_USER_SHCHANGENOTIFY, 1, ChangeNotifyEntry);
  434. end;
  435. with FDragDropFilesEx do
  436. begin
  437. ShellExtensions.DragDropHandler := True;
  438. end;
  439. end; {Create}
  440. destructor TDriveView.Destroy;
  441. var
  442. DriveStatusPair: TDriveStatusPair;
  443. begin
  444. if FChangeNotify <> 0 then SHChangeNotifyDeregister(FChangeNotify);
  445. Classes.DeallocateHWnd(FInternalWindowHandle);
  446. for DriveStatusPair in FDriveStatus do
  447. begin
  448. with DriveStatusPair.Value do
  449. begin
  450. if Assigned(DiscMonitor) then
  451. FreeAndNil(DiscMonitor);
  452. if Assigned(ChangeTimer) then
  453. FreeAndNil(ChangeTimer);
  454. end;
  455. UpdateDriveNotifications(DriveStatusPair.Key);
  456. end;
  457. FDriveStatus.Free;
  458. if Assigned(FFileOperator) then
  459. FFileOperator.Free;
  460. inherited Destroy;
  461. end; {Destroy}
  462. function TDriveView.CreateDriveStatus: TDriveStatus;
  463. begin
  464. Result := TDriveStatus.Create;
  465. with Result do
  466. begin
  467. Scanned := False;
  468. Verified := False;
  469. RootNode := nil;
  470. RootNodeIndex := -1;
  471. DiscMonitor := nil;
  472. DefaultDir := EmptyStr;
  473. {ChangeTimer: }
  474. ChangeTimer := TTimer.Create(Self);
  475. ChangeTimer.Interval := 0;
  476. ChangeTimer.Enabled := False;
  477. ChangeTimer.OnTimer := ChangeTimerOnTimer;
  478. DriveHandle := INVALID_HANDLE_VALUE;
  479. NotificationHandle := nil;
  480. end;
  481. end;
  482. procedure TDriveView.DriveRemoving(Drive: string);
  483. begin
  484. DriveRemoved(Drive);
  485. TerminateWatchThread(Drive);
  486. end;
  487. type
  488. PDevBroadcastHdr = ^TDevBroadcastHdr;
  489. TDevBroadcastHdr = record
  490. dbch_size: DWORD;
  491. dbch_devicetype: DWORD;
  492. dbch_reserved: DWORD;
  493. end;
  494. PDevBroadcastVolume = ^TDevBroadcastVolume;
  495. TDevBroadcastVolume = record
  496. dbcv_size: DWORD;
  497. dbcv_devicetype: DWORD;
  498. dbcv_reserved: DWORD;
  499. dbcv_unitmask: DWORD;
  500. dbcv_flags: WORD;
  501. end;
  502. PDEV_BROADCAST_HANDLE = ^DEV_BROADCAST_HANDLE;
  503. DEV_BROADCAST_HANDLE = record
  504. dbch_size : DWORD;
  505. dbch_devicetype : DWORD;
  506. dbch_reserved : DWORD;
  507. dbch_handle : THandle;
  508. dbch_hdevnotify : HDEVNOTIFY ;
  509. dbch_eventguid : TGUID;
  510. dbch_nameoffset : LongInt;
  511. dbch_data : Byte;
  512. end;
  513. PPItemIDList = ^PItemIDList;
  514. const
  515. DBT_DEVTYP_HANDLE = $00000006;
  516. DBT_CONFIGCHANGED = $0018;
  517. DBT_DEVICEARRIVAL = $8000;
  518. DBT_DEVICEQUERYREMOVE = $8001;
  519. DBT_DEVICEREMOVEPENDING = $8003;
  520. DBT_DEVICEREMOVECOMPLETE = $8004;
  521. DBT_DEVTYP_VOLUME = $00000002;
  522. // WORKAROUND Declaration in Winapi.ShlObj.pas is wrong
  523. function SHChangeNotification_Lock(hChange: THandle; dwProcId: DWORD;
  524. var PPidls: PPItemIDList; var plEvent: Longint): THANDLE; stdcall;
  525. external 'shell32.dll' name 'SHChangeNotification_Lock';
  526. procedure TDriveView.InternalWndProc(var Msg: TMessage);
  527. var
  528. DeviceType: DWORD;
  529. UnitMask: DWORD;
  530. DeviceHandle: THandle;
  531. Drive: Char;
  532. DriveStatusPair: TDriveStatusPair;
  533. PPIDL: PPItemIDList;
  534. Event: LONG;
  535. Lock: THandle;
  536. begin
  537. with Msg do
  538. begin
  539. if Msg = WM_USER_SHCHANGENOTIFY then
  540. begin
  541. Lock := SHChangeNotification_Lock(wParam, lParam, PPIDL, Event);
  542. try
  543. if (Event = SHCNE_RENAMEFOLDER) or // = drive rename
  544. (Event = SHCNE_MEDIAINSERTED) or // also bitlocker drive unlock (also sends SHCNE_UPDATEDIR)
  545. (Event = SHCNE_MEDIAREMOVED) then
  546. begin
  547. ScheduleDriveRefresh;
  548. end;
  549. finally
  550. SHChangeNotification_Unlock(Lock);
  551. end;
  552. end
  553. else
  554. if Msg = WM_DEVICECHANGE then
  555. begin
  556. if (wParam = DBT_CONFIGCHANGED) or
  557. (wParam = DBT_DEVICEARRIVAL) or
  558. (wParam = DBT_DEVICEREMOVECOMPLETE) then
  559. begin
  560. ScheduleDriveRefresh;
  561. end
  562. else
  563. if (wParam = DBT_DEVICEQUERYREMOVE) or
  564. (wParam = DBT_DEVICEREMOVEPENDING) then
  565. begin
  566. DeviceType := PDevBroadcastHdr(lParam)^.dbch_devicetype;
  567. // This is specifically for VeraCrypt.
  568. // For normal drives, see DBT_DEVTYP_HANDLE below
  569. // (and maybe now that we have generic implementation, this specific code for VeraCrypt might not be needed anymore)
  570. if DeviceType = DBT_DEVTYP_VOLUME then
  571. begin
  572. UnitMask := PDevBroadcastVolume(lParam)^.dbcv_unitmask;
  573. Drive := FirstDrive;
  574. while UnitMask > 0 do
  575. begin
  576. if UnitMask and $01 <> 0 then
  577. begin
  578. DriveRemoving(Drive);
  579. end;
  580. UnitMask := UnitMask shr 1;
  581. Drive := Chr(Ord(Drive) + 1);
  582. end;
  583. end
  584. else
  585. if DeviceType = DBT_DEVTYP_HANDLE then
  586. begin
  587. DeviceHandle := PDEV_BROADCAST_HANDLE(lParam)^.dbch_handle;
  588. for DriveStatusPair in FDriveStatus do
  589. if DriveStatusPair.Value.DriveHandle = DeviceHandle then
  590. begin
  591. DriveRemoving(DriveStatusPair.Key);
  592. end;
  593. end;
  594. end;
  595. end
  596. else
  597. if Msg = WM_TIMER then
  598. begin
  599. CancelDriveRefresh;
  600. try
  601. //DriveInfo.Load;
  602. RefreshRootNodes(dsAll or dvdsRereadAllways);
  603. if Assigned(OnRefreshDrives) then
  604. OnRefreshDrives(Self);
  605. except
  606. Application.HandleException(Self);
  607. end;
  608. end;
  609. Result := DefWindowProc(FInternalWindowHandle, Msg, wParam, lParam);
  610. end;
  611. end;
  612. procedure TDriveView.CancelDriveRefresh;
  613. begin
  614. KillTimer(FInternalWindowHandle, 1);
  615. end;
  616. procedure TDriveView.ScheduleDriveRefresh;
  617. begin
  618. CancelDriveRefresh;
  619. // Delay refreshing drives for a sec.
  620. // Particularly with CD/DVD drives, if we query display name
  621. // immediately after receiving DBT_DEVICEARRIVAL, we do not get media label.
  622. // Actually one sec does not help usually, but we do not want to wait any longer,
  623. // because we want to add USB drives asap.
  624. // And this problem might be solved now by SHChangeNotifyRegister/SHCNE_RENAMEFOLDER.
  625. SetTimer(FInternalWindowHandle, 1, MSecsPerSec, nil);
  626. end;
  627. procedure TDriveView.CreateWnd;
  628. var
  629. DriveStatus: TDriveStatus;
  630. begin
  631. inherited;
  632. if Assigned(PopupMenu) then
  633. PopupMenu.Autopopup := False;
  634. OLECheck(SHGetDesktopFolder(FDesktop));
  635. FDragDropFilesEx.SourceEffects := [deCopy, deMove, deLink];
  636. FDragDropFilesEx.TargetEffects := [deCopy, deMove, deLink];
  637. if FPrevSelectedIndex >= 0 then
  638. begin
  639. FPrevSelected := Items[FPrevSelectedIndex];
  640. FPrevSelectedIndex := -1;
  641. end;
  642. for DriveStatus in FDriveStatus.Values do
  643. with DriveStatus do
  644. begin
  645. if RootNodeIndex >= 0 then
  646. begin
  647. RootNode := Items[RootNodeIndex];
  648. RootNodeIndex := -1;
  649. end;
  650. end;
  651. end; {CreateWnd}
  652. procedure TDriveView.DestroyWnd;
  653. var
  654. DriveStatus: TDriveStatus;
  655. begin
  656. if CreateWndRestores and (Items.Count > 0) and (csRecreating in ControlState) then
  657. begin
  658. FPrevSelectedIndex := -1;
  659. if Assigned(FPrevSelected) then
  660. begin
  661. FPrevSelectedIndex := FPrevSelected.AbsoluteIndex;
  662. FPrevSelected := nil;
  663. end;
  664. for DriveStatus in FDriveStatus.Values do
  665. with DriveStatus do
  666. begin
  667. RootNodeIndex := -1;
  668. if Assigned(RootNode) then
  669. begin
  670. RootNodeIndex := RootNode.AbsoluteIndex;
  671. RootNode := nil;
  672. end;
  673. end;
  674. end;
  675. inherited;
  676. end;
  677. function TDriveView.GetFQPIDL(Node: TTreeNode): PItemIDList;
  678. var
  679. Eaten: ULONG;
  680. shAttr: ULONG;
  681. begin
  682. Result := nil;
  683. if Assigned(Node) then
  684. begin
  685. shAttr := 0;
  686. FDesktop.ParseDisplayName(FParentForm.Handle, nil, PChar(NodePathName(Node)), Eaten,
  687. Result, shAttr);
  688. end;
  689. end; {GetFQPIDL}
  690. function TDriveView.NodeColor(Node: TTreeNode): TColor;
  691. begin
  692. Result := clDefaultItemColor;
  693. with TNodeData(Node.Data) do
  694. if not Node.Selected then
  695. begin
  696. {Colored display of compressed directories:}
  697. if (Attr and FILE_ATTRIBUTE_COMPRESSED) <> 0 then
  698. begin
  699. if SupportsDarkMode and DarkMode then Result := clSkyBlue
  700. else Result := clBlue;
  701. end
  702. else
  703. {Dimmed display, if hidden-atrribut set:}
  704. if FDimmHiddenDirs and ((Attr and FILE_ATTRIBUTE_HIDDEN) <> 0) then
  705. Result := clGrayText
  706. end;
  707. end;
  708. function TDriveView.GetCustomDirView: TCustomDirView;
  709. begin
  710. Result := DirView;
  711. end;
  712. procedure TDriveView.SetCustomDirView(Value: TCustomDirView);
  713. begin
  714. DirView := Value as TDirView;
  715. end;
  716. function TDriveView.NodePath(Node: TTreeNode): string;
  717. var
  718. ParentNode: TTreeNode;
  719. begin
  720. if not Assigned(Node) then
  721. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDirPath']));
  722. Result := GetDirName(Node);
  723. ParentNode := Node.Parent;
  724. while (ParentNode <> nil) and (ParentNode.Level >= 0) do
  725. begin
  726. if ParentNode.Level > 0 then
  727. Result := GetDirName(ParentNode) + '\' + Result
  728. else
  729. Result := GetDirName(ParentNode) + Result;
  730. ParentNode := ParentNode.Parent;
  731. end;
  732. if IsRootPath(Result) then
  733. Result := ExcludeTrailingBackslash(Result);
  734. end;
  735. {NodePathName: Returns the complete path to Node with trailing backslash on rootnodes:
  736. C:\ ,C:\WINDOWS, C:\WINDOWS\SYSTEM }
  737. function TDriveView.NodePathName(Node: TTreeNode): string;
  738. begin
  739. Result := NodePath(Node);
  740. if IsRootPath(Result) then
  741. Result := IncludeTrailingBackslash(Result);
  742. end; {NodePathName}
  743. function TDriveView.NodeIsRecycleBin(Node: TTreeNode): Boolean;
  744. begin
  745. Result := TNodeData(Node.Data).IsRecycleBin;
  746. end;
  747. function TDriveView.NodePathExists(Node: TTreeNode): Boolean;
  748. begin
  749. Result := DirectoryExists(NodePathName(Node));
  750. end;
  751. function TDriveView.CanEdit(Node: TTreeNode): Boolean;
  752. begin
  753. Result := inherited CanEdit(Node) or FForceRename;
  754. if Result then
  755. begin
  756. Result := Assigned(Node.Parent) and
  757. (not TNodeData(Node.Data).IsRecycleBin) and
  758. (not ReadOnly) and
  759. (FDragDropFilesEx.DragDetectStatus <> ddsDrag) and
  760. ((TNodeData(Node.Data).Attr and (faReadOnly or faSysFile)) = 0) and
  761. (UpperCase(Node.Text) = UpperCase(GetDirName(Node)));
  762. end;
  763. FForceRename := False;
  764. end; {CanEdit}
  765. procedure TDriveView.Edit(const Item: TTVItem);
  766. var
  767. SRec: TSearchRec;
  768. Node: TTreeNode;
  769. Info: string;
  770. i: Integer;
  771. begin
  772. Node := GetNodeFromHItem(Item);
  773. if (Length(Item.pszText) > 0) and (Item.pszText <> Node.Text) then
  774. begin
  775. if StrContains(coInvalidDosChars, Item.pszText) then
  776. begin
  777. Info := coInvalidDosChars;
  778. for i := Length(Info) downto 1 do
  779. System.Insert(Space, Info, i);
  780. if Length(Item.pszText) > 0 then
  781. raise EInvalidDirName.Create(SErrorInvalidName + Space + Info);
  782. Exit;
  783. end;
  784. StopWatchThread;
  785. if Assigned(DirView) then
  786. DirView.StopWatchThread;
  787. with FFileOperator do
  788. begin
  789. Flags := FileOperatorDefaultFlags + [foNoConfirmation];
  790. Operation := foRename;
  791. OperandFrom.Clear;
  792. OperandTo.Clear;
  793. OperandFrom.Add(NodePath(Node));
  794. OperandTo.Add(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText);
  795. end;
  796. try
  797. if FFileOperator.Execute then
  798. begin
  799. Node.Text := Item.pszText;
  800. TNodeData(Node.Data).DirName := Item.pszText;
  801. if FindFirst(ApiPath(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText),
  802. faAnyFile, SRec) = 0 then
  803. begin
  804. TNodeData(Node.Data).ShortName := string(SRec.FindData.cAlternateFileName);
  805. end;
  806. FindClose(SRec);
  807. SortChildren(Node.Parent, False);
  808. inherited;
  809. end
  810. else
  811. begin
  812. if FileOrDirExists(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText) then
  813. Info := SErrorRenameFileExists + Item.pszText
  814. else
  815. Info := SErrorRenameFile + Item.pszText;
  816. MessageBeep(MB_ICONHAND);
  817. if MessageDlg(FormatLastOSError(Info), mtError, [mbOK, mbAbort], 0) = mrOK then
  818. begin
  819. FLastRenameName := Item.pszText;
  820. FRenameNode := Node;
  821. PostMessage(Self.Handle, WM_USER_RENAME, 0, 0);
  822. end;
  823. end;
  824. finally
  825. StartWatchThread;
  826. if Assigned(DirView) then
  827. begin
  828. DirView.Reload2;
  829. DirView.StartWatchThread;
  830. end;
  831. end;
  832. end;
  833. end; {Edit}
  834. procedure TDriveView.WMUserRename(var Message: TMessage);
  835. begin
  836. if Assigned(FRenameNode) then
  837. begin
  838. FForceRename := True;
  839. TreeView_EditLabel(Handle, FRenameNode.ItemID);
  840. SetWindowText(TreeView_GetEditControl(Self.Handle), PChar(FLastRenameName));
  841. FRenameNode := nil;
  842. end;
  843. end; {WMUserRename}
  844. function TDriveView.CanExpand(Node: TTreeNode): Boolean;
  845. var
  846. SubNode: TTreeNode;
  847. Drive: string;
  848. SaveCursor: TCursor;
  849. begin
  850. Result := inherited CanExpand(Node);
  851. Drive := GetDriveToNode(Node);
  852. if Node.HasChildren then
  853. begin
  854. if (Node.Level = 0) and
  855. (not GetDriveStatus(Drive).Scanned) and
  856. DriveInfo.IsFixedDrive(Drive) then
  857. begin
  858. SubNode := Node.GetFirstChild;
  859. if not Assigned(SubNode) then
  860. begin
  861. ScanDrive(Drive);
  862. SubNode := Node.GetFirstChild;
  863. Node.HasChildren := Assigned(SubNode);
  864. Result := Node.HasChildren;
  865. if not Assigned(GetDriveStatus(Drive).DiscMonitor) then
  866. CreateWatchThread(Drive);
  867. end;
  868. end
  869. else
  870. begin
  871. SaveCursor := Screen.Cursor;
  872. Screen.Cursor := crHourGlass;
  873. try
  874. if (not TNodeData(Node.Data).Scanned) and DoScanDir(Node) then
  875. begin
  876. ReadSubDirs(Node, DriveInfo.Get(Drive).DriveType);
  877. end;
  878. finally
  879. Screen.Cursor := SaveCursor;
  880. end;
  881. end;
  882. end;
  883. end; {CanExpand}
  884. procedure TDriveView.GetImageIndex(Node: TTreeNode);
  885. begin
  886. if TNodeData(Node.Data).IconEmpty then
  887. SetImageIndex(Node);
  888. inherited;
  889. end; {GetImageIndex}
  890. procedure TDriveView.Loaded;
  891. begin
  892. inherited;
  893. {Create the drive nodes:}
  894. RefreshRootNodes(dsDisplayName or dvdsFloppy);
  895. {Set the initial directory:}
  896. if (Length(FDirectory) > 0) and DirectoryExists(FDirectory) then
  897. Directory := FDirectory;
  898. FCreating := False;
  899. end; {Loaded}
  900. function TDriveView.CreateNode: TTreeNode;
  901. begin
  902. Result := TDriveTreeNode.Create(Items);
  903. end;
  904. procedure TDriveView.Delete(Node: TTreeNode);
  905. var
  906. NodeData: TNodeData;
  907. begin
  908. if Node = FPrevSelected then
  909. FPrevSelected := nil;
  910. NodeData := nil;
  911. if Assigned(Node) and Assigned(Node.Data) then
  912. NodeData := TNodeData(Node.Data);
  913. Node.Data := nil;
  914. inherited;
  915. if Assigned(NodeData) and not (csRecreating in ControlState) then
  916. begin
  917. NodeData.Destroy;
  918. end;
  919. end; {OnDelete}
  920. procedure TDriveView.KeyPress(var Key: Char);
  921. begin
  922. inherited;
  923. if Assigned(Selected) then
  924. begin
  925. if Pos(Key, coInvalidDosChars) <> 0 then
  926. begin
  927. Beep;
  928. Key := #0;
  929. end;
  930. end;
  931. end; {KeyPress}
  932. function TDriveView.CanChange(Node: TTreeNode): Boolean;
  933. var
  934. Path: string;
  935. Drive: string;
  936. begin
  937. Result := inherited CanChange(Node);
  938. if not Reading and not (csRecreating in ControlState) then
  939. begin
  940. if Result and Assigned(Node) then
  941. begin
  942. Path := NodePathName(Node);
  943. if Path <> FLastDir then
  944. begin
  945. Drive := DriveInfo.GetDriveKey(Path);
  946. DriveInfo.ReadDriveStatus(Drive, dsSize or dsImageIndex);
  947. if not DriveInfo.Get(Drive).DriveReady then
  948. begin
  949. MessageDlg(Format(SDriveNotReady, [Drive]), mtError, [mbOK], 0);
  950. Result := False;
  951. end
  952. else
  953. try
  954. CheckCanOpenDirectory(Path);
  955. except
  956. Application.HandleException(Self);
  957. Result := False;
  958. end;
  959. end;
  960. end;
  961. if Result and (csDestroying in ComponentState) then
  962. begin
  963. Result := False;
  964. end;
  965. if Result and
  966. (not FCanChange) and
  967. Assigned(Node) and
  968. Assigned(Node.Data) and
  969. Assigned(Selected) and
  970. Assigned(Selected.Data) then
  971. begin
  972. DropTarget := Node;
  973. Result := False;
  974. end
  975. else
  976. begin
  977. DropTarget := nil;
  978. end;
  979. end;
  980. end; {CanChange}
  981. procedure TDriveView.Change(Node: TTreeNode);
  982. var
  983. Drive: string;
  984. OldSerial: DWORD;
  985. NewDir: string;
  986. PrevDrive: string;
  987. begin
  988. if not Reading and not (csRecreating in ControlState) then
  989. begin
  990. if Assigned(Node) then
  991. begin
  992. NewDir := NodePathName(Node);
  993. if NewDir <> FLastDir then
  994. begin
  995. Drive := DriveInfo.GetDriveKey(NewDir);
  996. if Length(FLastDir) > 0 then
  997. PrevDrive := DriveInfo.GetDriveKey(FLastDir)
  998. else
  999. PrevDrive := '';
  1000. FChangeFlag := True;
  1001. FLastDir := NewDir;
  1002. OldSerial := DriveInfo.Get(Drive).DriveSerial;
  1003. DriveInfo.ReadDriveStatus(Drive, dsSize or dsImageIndex);
  1004. with DriveInfo.Get(Drive) do
  1005. begin
  1006. if Assigned(FDirView) and (FDirView.Path <> NewDir) then
  1007. FDirView.Path := NewDir;
  1008. if DriveReady then
  1009. begin
  1010. if not DirectoryExists(NewDir) then
  1011. begin
  1012. ValidateDirectory(GetDriveStatus(Drive).RootNode);
  1013. Exit;
  1014. end;
  1015. GetDriveStatus(Drive).DefaultDir := IncludeTrailingBackslash(NewDir);
  1016. if PrevDrive <> Drive then
  1017. begin
  1018. if (PrevDrive <> '') and
  1019. (DriveInfo.Get(PrevDrive).DriveType = DRIVE_REMOVABLE) then
  1020. begin
  1021. TerminateWatchThread(PrevDrive);
  1022. end;
  1023. {Drive serial has changed or is missing: allways reread the drive:}
  1024. if (DriveSerial <> OldSerial) or (DriveSerial = 0) then
  1025. begin
  1026. if TNodeData(GetDriveStatus(Drive).RootNode.Data).Scanned then
  1027. ScanDrive(Drive);
  1028. end;
  1029. end;
  1030. StartWatchThread;
  1031. end
  1032. else {Drive not ready:}
  1033. begin
  1034. GetDriveStatus(Drive).RootNode.DeleteChildren;
  1035. GetDriveStatus(Drive).DefaultDir := EmptyStr;
  1036. end;
  1037. end;
  1038. end;
  1039. if (not Assigned(FPrevSelected)) or (not FPrevSelected.HasAsParent(Node)) then
  1040. Node.Expand(False);
  1041. FPrevSelected := Node;
  1042. ValidateCurrentDirectoryIfNotMonitoring;
  1043. end;
  1044. end;
  1045. inherited;
  1046. end; {Change}
  1047. procedure TDriveView.SetImageIndex(Node: TTreeNode);
  1048. var
  1049. FileInfo: TShFileInfo;
  1050. Drive, NodePath: string;
  1051. begin
  1052. if Assigned(Node) and TNodeData(Node.Data).IconEmpty then
  1053. begin
  1054. NodePath := NodePathName(Node);
  1055. Drive := DriveInfo.GetDriveKey(NodePath);
  1056. if Node.Level = 0 then
  1057. begin
  1058. with DriveInfo.Get(Drive) do
  1059. begin
  1060. if ImageIndex = 0 then
  1061. begin
  1062. DriveInfo.ReadDriveStatus(Drive, dsImageIndex);
  1063. Node.ImageIndex := DriveInfo.Get(Drive).ImageIndex;
  1064. end
  1065. else Node.ImageIndex := ImageIndex;
  1066. Node.SelectedIndex := Node.ImageIndex;
  1067. end;
  1068. end
  1069. else
  1070. begin
  1071. if DriveInfo.Get(Drive).DriveType = DRIVE_REMOTE then
  1072. begin
  1073. Node.ImageIndex := StdDirIcon;
  1074. Node.SelectedIndex := StdDirSelIcon;
  1075. end
  1076. else
  1077. begin
  1078. try
  1079. SHGetFileInfo(PChar(NodePath), 0, FileInfo, SizeOf(FileInfo),
  1080. SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  1081. if (FileInfo.iIcon < Images.Count) and (FileInfo.iIcon > 0) then
  1082. begin
  1083. Node.ImageIndex := FileInfo.iIcon;
  1084. SHGetFileInfo(PChar(NodePath), 0, FileInfo, SizeOf(FileInfo),
  1085. SHGFI_SYSICONINDEX or SHGFI_SMALLICON or SHGFI_OPENICON);
  1086. Node.SelectedIndex := FileInfo.iIcon;
  1087. end
  1088. else
  1089. begin
  1090. Node.ImageIndex := StdDirIcon;
  1091. Node.SelectedIndex := StdDirSelIcon;
  1092. end;
  1093. except
  1094. Node.ImageIndex := StdDirIcon;
  1095. Node.SelectedIndex := StdDirSelIcon;
  1096. end;
  1097. end;
  1098. end;
  1099. end; {IconEmpty}
  1100. TNodeData(Node.Data).IconEmpty := False;
  1101. end; {SetImageIndex}
  1102. function TDriveView.GetDriveText(Drive: string): string;
  1103. begin
  1104. if FShowVolLabel and (Length(DriveInfo.GetPrettyName(Drive)) > 0) then
  1105. begin
  1106. case FVolDisplayStyle of
  1107. doPrettyName: Result := DriveInfo.GetPrettyName(Drive);
  1108. doDisplayName: Result := DriveInfo.GetDisplayName(Drive);
  1109. end; {Case}
  1110. end
  1111. else
  1112. begin
  1113. Result := DriveInfo.GetSimpleName(Drive);
  1114. end;
  1115. end; {GetDriveText}
  1116. procedure TDriveView.GetNodeShellAttr(ParentNode: TTreeNode; NodeData: TNodeData; GetAttr: Boolean);
  1117. var
  1118. ParentFolder: IShellFolder;
  1119. ParentData: TNodeData;
  1120. begin
  1121. NodeData.shAttr := 0;
  1122. if GetAttr then
  1123. begin
  1124. if Assigned(ParentNode) then
  1125. begin
  1126. ParentData := TNodeData(ParentNode.Data);
  1127. if not Assigned(ParentData) then
  1128. begin
  1129. Assert(False);
  1130. ParentFolder := nil;
  1131. end
  1132. else
  1133. begin
  1134. if not Assigned(ParentData.ShellFolder) then
  1135. begin
  1136. GetNodeShellAttr(ParentNode.Parent, ParentData, GetAttr);
  1137. end;
  1138. ParentFolder := ParentData.ShellFolder;
  1139. end;
  1140. end
  1141. else
  1142. begin
  1143. ParentFolder := FDesktop;
  1144. end;
  1145. if Assigned(ParentFolder) and Assigned(NodeData) then
  1146. begin
  1147. if not Assigned(NodeData.PIDL) then
  1148. NodeData.PIDL := PIDL_GetFromParentFolder(ParentFolder, PChar(NodeData.DirName));
  1149. if Assigned(NodeData.PIDL) then
  1150. begin
  1151. NodeData.shAttr := SFGAO_CONTENTSMASK;
  1152. // Previously we would also make use of SFGAO_SHARE to display a share overlay.
  1153. // But for directories, Windows File Explorer does not display the overlay anymore (probably since Vista).
  1154. // And for drives (where Explorer does display the overlay), it did not work ever since we use "desktop"
  1155. // (and not "workspace" as before) to resolve drive interface (see Bug 1717).
  1156. if not Succeeded(ShellFolderGetAttributesOfWithTimeout(ParentFolder, 1, NodeData.PIDL, NodeData.shAttr, MSecsPerSec)) then
  1157. begin
  1158. NodeData.shAttr := 0;
  1159. end;
  1160. if not Assigned(NodeData.ShellFolder) then
  1161. begin
  1162. ParentFolder.BindToObject(NodeData.PIDL, nil, IID_IShellFolder, Pointer(NodeData.ShellFolder));
  1163. end;
  1164. end
  1165. end;
  1166. end;
  1167. if NodeData.shAttr = 0 then
  1168. begin
  1169. // If we cannot resolve attrs, we do not want to assume that the folder has no subfolders,
  1170. // as that will make us scan the folder.
  1171. NodeData.shAttr := SFGAO_HASSUBFOLDER;
  1172. end;
  1173. end; {GetNodeAttr}
  1174. function CompareDrive(List: TStringList; Index1, Index2: Integer): Integer;
  1175. var
  1176. Drive1, Drive2: string;
  1177. RealDrive1, RealDrive2: Boolean;
  1178. begin
  1179. Drive1 := List[Index1];
  1180. Drive2 := List[Index2];
  1181. RealDrive1 := DriveInfo.IsRealDrive(Drive1);
  1182. RealDrive2 := DriveInfo.IsRealDrive(Drive2);
  1183. if RealDrive1 = RealDrive2 then
  1184. begin
  1185. Result := CompareText(Drive1, Drive2);
  1186. end
  1187. else
  1188. if RealDrive1 and (not RealDrive2) then
  1189. begin
  1190. Result := -1;
  1191. end
  1192. else
  1193. begin
  1194. Result := 1;
  1195. end;
  1196. end;
  1197. function TDriveView.GetDrives: TStrings;
  1198. var
  1199. DriveStatusPair: TDriveStatusPair;
  1200. Drives: TStringList;
  1201. begin
  1202. Drives := TStringList.Create;
  1203. { We could iterate only .Keys here, but that crashes IDE for some reason }
  1204. for DriveStatusPair in FDriveStatus do
  1205. begin
  1206. Drives.Add(DriveStatusPair.Key);
  1207. end;
  1208. Drives.CustomSort(CompareDrive);
  1209. Result := Drives;
  1210. end;
  1211. procedure TDriveView.DriveRemoved(Drive: string);
  1212. var
  1213. NewDrive: Char;
  1214. begin
  1215. if (Directory <> '') and (Directory[1] = Drive) then
  1216. begin
  1217. if DriveInfo.IsRealDrive(Drive) then NewDrive := Drive[1]
  1218. else NewDrive := SystemDrive;
  1219. repeat
  1220. if NewDrive < SystemDrive then NewDrive := SystemDrive
  1221. else
  1222. if NewDrive = SystemDrive then NewDrive := LastDrive
  1223. else Dec(NewDrive);
  1224. DriveInfo.ReadDriveStatus(NewDrive, dsSize or dsImageIndex);
  1225. if NewDrive = Drive then
  1226. begin
  1227. Break;
  1228. end;
  1229. if DriveInfo.Get(NewDrive).Valid and DriveInfo.Get(NewDrive).DriveReady and Assigned(GetDriveStatus(NewDrive).RootNode) then
  1230. begin
  1231. Directory := NodePathName(GetDriveStatus(NewDrive).RootNode);
  1232. break;
  1233. end;
  1234. until False;
  1235. if not Assigned(Selected) then
  1236. begin
  1237. Directory := NodePathName(GetDriveStatus(SystemDrive).RootNode);
  1238. end;
  1239. end;
  1240. end;
  1241. procedure TDriveView.RefreshRootNodes(dsFlags: Integer);
  1242. var
  1243. Drives: TStrings;
  1244. NewText: string;
  1245. SaveCursor: TCursor;
  1246. WasValid: Boolean;
  1247. NodeData: TNodeData;
  1248. DriveStatus: TDriveStatus;
  1249. NextDriveNode: TTreeNode;
  1250. Index: Integer;
  1251. Drive: string;
  1252. GetAttr: Boolean;
  1253. begin
  1254. SaveCursor := Screen.Cursor;
  1255. Screen.Cursor := crHourGlass;
  1256. Drives := nil;
  1257. try
  1258. Drives := GetDrives;
  1259. NextDriveNode := nil;
  1260. for Index := Drives.Count - 1 downto 0 do
  1261. begin
  1262. Drive := Drives[Index];
  1263. DriveStatus := GetDriveStatus(Drive);
  1264. if ((dsFlags and dvdsFloppy) <> 0) or DriveInfo.IsFixedDrive(Drive) then
  1265. begin
  1266. with DriveInfo.Get(Drive) do
  1267. begin
  1268. WasValid := Assigned(DriveStatus.RootNode);
  1269. end;
  1270. if ((dsFlags and dvdsReReadAllways) = 0) and
  1271. (Length(DriveInfo.Get(Drive).DisplayName) > 0) then
  1272. dsFlags := dsFlags and (not dsDisplayName);
  1273. DriveInfo.ReadDriveStatus(Drive, dsFlags);
  1274. with DriveInfo.Get(Drive), DriveStatus do
  1275. begin
  1276. if Valid then
  1277. begin
  1278. if not WasValid then
  1279. {New drive has arrived: insert new rootnode:}
  1280. begin
  1281. { Create root directory node }
  1282. NodeData := TNodeData.Create;
  1283. NodeData.DirName := DriveInfo.GetDriveRoot(Drive);
  1284. NodeData.ShortName := NodeData.DirName;
  1285. {Get the shared attributes:}
  1286. GetAttr :=
  1287. DriveInfo.IsFixedDrive(Drive) and (DriveType <> DRIVE_REMOVABLE) and
  1288. ((DriveType <> DRIVE_REMOTE) or GetNetWorkConnected(Drive));
  1289. GetNodeShellAttr(nil, NodeData, GetAttr);
  1290. if Assigned(NextDriveNode) then
  1291. RootNode := Items.InsertObject(NextDriveNode, '', NodeData)
  1292. else
  1293. RootNode := Items.AddObject(nil, '', NodeData);
  1294. RootNode.Text := GetDisplayName(RootNode);
  1295. RootNode.HasChildren := True;
  1296. Scanned := False;
  1297. Verified := False;
  1298. end
  1299. else
  1300. if RootNode.ImageIndex <> DriveInfo.Get(Drive).ImageIndex then
  1301. begin {WasValid = True}
  1302. RootNode.ImageIndex := DriveInfo.Get(Drive).ImageIndex;
  1303. RootNode.SelectedIndex := DriveInfo.Get(Drive).ImageIndex;
  1304. end;
  1305. if Assigned(RootNode) then
  1306. begin
  1307. NewText := GetDisplayName(RootNode);
  1308. if RootNode.Text <> NewText then
  1309. RootNode.Text := NewText;
  1310. end;
  1311. end
  1312. else
  1313. if WasValid then
  1314. {Drive has been removed => delete rootnode:}
  1315. begin
  1316. DriveRemoved(Drive);
  1317. Scanned := False;
  1318. Verified := False;
  1319. RootNode.Delete;
  1320. RootNode := nil;
  1321. end;
  1322. end;
  1323. end;
  1324. if Assigned(DriveStatus.RootNode) then
  1325. NextDriveNode := DriveStatus.RootNode;
  1326. end;
  1327. finally
  1328. Screen.Cursor := SaveCursor;
  1329. Drives.Free;
  1330. end;
  1331. end; {RefreshRootNodes}
  1332. function TDriveView.AddChildNode(ParentNode: TTreeNode; SRec: TSearchRec): TTreeNode;
  1333. var
  1334. NewNode: TTreeNode;
  1335. NodeData: TNodeData;
  1336. GetAttr: Boolean;
  1337. begin
  1338. NodeData := TNodeData.Create;
  1339. NodeData.Attr := SRec.Attr;
  1340. NodeData.DirName := SRec.Name;
  1341. NodeData.ShortName := SRec.FindData.cAlternateFileName;
  1342. NodeData.FIsRecycleBin :=
  1343. (SRec.Attr and faSysFile <> 0) and
  1344. (ParentNode.Level = 0) and
  1345. (SameText(SRec.Name, 'RECYCLED') or
  1346. SameText(SRec.Name, 'RECYCLER') or
  1347. SameText(SRec.Name, '$RECYCLE.BIN'));
  1348. { query content attributes ("has subfolder") only if tree view is visible }
  1349. { to avoid unnecessary scan of subfolders (which may take some time) }
  1350. { if tree view is not visible anyway }
  1351. GetAttr :=
  1352. Visible and
  1353. (GetDriveTypeToNode(ParentNode) <> DRIVE_REMOTE);
  1354. GetNodeShellAttr(ParentNode, NodeData, GetAttr);
  1355. NewNode := Self.Items.AddChildObject(ParentNode, '', NodeData);
  1356. NewNode.Text := GetDisplayName(NewNode);
  1357. Result := NewNode;
  1358. end; {AddChildNode}
  1359. function TDriveView.GetDriveStatus(Drive: string): TDriveStatus;
  1360. begin
  1361. if not FDriveStatus.TryGetValue(Drive, Result) then
  1362. begin
  1363. Result := CreateDriveStatus;
  1364. FDriveStatus.Add(Drive, Result);
  1365. RefreshRootNodes(dsAll or dvdsRereadAllways);
  1366. if Assigned(OnRefreshDrives) then
  1367. OnRefreshDrives(Self);
  1368. end;
  1369. end; {GetDriveStatus}
  1370. function TDriveView.DoScanDir(FromNode: TTreeNode): Boolean;
  1371. begin
  1372. Result := not TNodeData(FromNode.Data).IsRecycleBin;
  1373. end; {DoScanDir}
  1374. function TDriveView.DirAttrMask: Integer;
  1375. begin
  1376. Result := faDirectory or faSysFile;
  1377. if ShowHiddenDirs then
  1378. Result := Result or faHidden;
  1379. end;
  1380. procedure TDriveView.ScanDrive(Drive: string);
  1381. var
  1382. DosError: Integer;
  1383. RootNode: TTreeNode;
  1384. SaveCursor: TCursor;
  1385. procedure ScanPath(const Path: string; ParentNode: TTreeNode);
  1386. var
  1387. SRec: TSearchRec;
  1388. SubNode: TTreeNode;
  1389. begin
  1390. if not DoScanDir(ParentNode) then
  1391. Exit;
  1392. DosError := FindFirst(ApiPath(Path), DirAttrMask, Srec);
  1393. while DosError = 0 do
  1394. begin
  1395. if (SRec.Name <> '.') and
  1396. (SRec.Name <> '..') and
  1397. (SRec.Attr and faDirectory <> 0) then
  1398. begin
  1399. if (SRec.Attr And faDirectory) <> 0 then
  1400. begin { Scan subdirectory }
  1401. SubNode := AddChildNode(ParentNode, SRec);
  1402. TNodeData(SubNode.Data).Scanned := True;
  1403. ScanPath(ExtractFilePath(Path) + SRec.Name + '\*.*', SubNode);
  1404. if not FContinue then
  1405. Break;
  1406. end;
  1407. end;
  1408. DosError := FindNext(SRec);
  1409. end;
  1410. FindClose(Srec);
  1411. if (Items.Count mod 10) = 0 then
  1412. Application.ProcessMessages;
  1413. if not FContinue then
  1414. Exit;
  1415. end; {ScanPath}
  1416. begin {ScanDrive}
  1417. with Self.Items do
  1418. begin
  1419. FContinue := True;
  1420. if not FFullDriveScan then
  1421. begin
  1422. ValidateDirectory(FindNodeToPath(DriveInfo.GetDriveRoot(Drive)));
  1423. GetDriveStatus(Drive).Scanned := True;
  1424. GetDriveStatus(Drive).Verified := False;
  1425. end
  1426. else
  1427. begin
  1428. SaveCursor := Screen.Cursor;
  1429. Screen.Cursor := crHourGlass;
  1430. Items.BeginUpdate;
  1431. try
  1432. RootNode := GetDriveStatus(Drive).RootNode;
  1433. if not Assigned(RootNode) then Exit;
  1434. iF RootNode.HasChildren then
  1435. RootNode.DeleteChildren;
  1436. ScanPath(DriveInfo.GetDriveRoot(Drive) + '*.*', RootNode); { scan subdirectories of rootdir}
  1437. TNodeData(RootNode.Data).Scanned := True;
  1438. GetDriveStatus(Drive).Scanned := True;
  1439. GetDriveStatus(Drive).Verified := True;
  1440. finally
  1441. SortChildren(GetDriveStatus(Drive).RootNode, True);
  1442. EndUpdate;
  1443. end;
  1444. RootNode.Expand(False);
  1445. Screen.Cursor := SaveCursor;
  1446. end;
  1447. end;
  1448. end; {ScanDrive}
  1449. function TDriveView.DoFindNodeToPath(Path: string; ExistingOnly: Boolean): TTreeNode;
  1450. function SearchSubDirs(ParentNode: TTreeNode; Path: string): TTreeNode; forward;
  1451. function DoSearchSubDirs(ParentNode: TTreeNode; Path: string): TTreeNode;
  1452. var
  1453. i: Integer;
  1454. Node: TTreeNode;
  1455. Dir: string;
  1456. begin
  1457. {Extract first directory from path:}
  1458. i := Pos('\', Path);
  1459. if i = 0 then
  1460. i := Length(Path);
  1461. Dir := System.Copy(Path, 1, i);
  1462. System.Delete(Path, 1, i);
  1463. if Dir[Length(Dir)] = '\' then
  1464. SetLength(Dir, Pred(Length(Dir)));
  1465. Node := ParentNode.GetFirstChild;
  1466. if (not Assigned(Node)) and (not ExistingOnly) then
  1467. begin
  1468. ValidateDirectoryEx(ParentNode, rsRecursiveExisting, True);
  1469. Node := ParentNode.GetFirstChild;
  1470. end;
  1471. Result := nil;
  1472. while Assigned(Node) do
  1473. begin
  1474. if (UpperCase(GetDirName(Node)) = Dir) or (TNodeData(Node.Data).ShortName = Dir) then
  1475. begin
  1476. if Length(Path) > 0 then
  1477. begin
  1478. Result := SearchSubDirs(Node, Path)
  1479. end
  1480. else
  1481. begin
  1482. Result := Node;
  1483. end;
  1484. Exit;
  1485. end;
  1486. Node := ParentNode.GetNextChild(Node);
  1487. end;
  1488. end;
  1489. function SearchSubDirs(ParentNode: TTreeNode; Path: string): TTreeNode;
  1490. begin
  1491. Result := nil;
  1492. if Length(Path) > 0 then
  1493. begin
  1494. if (not TNodeData(ParentNode.Data).Scanned) and (not ExistingOnly) then
  1495. begin
  1496. ReadSubDirs(ParentNode, GetDriveTypetoNode(ParentNode));
  1497. end;
  1498. // Factored out of DoSearchSubDirs is remnant of Bug 956 superceded by Bug 1320
  1499. Result := DoSearchSubDirs(ParentNode, Path);
  1500. end;
  1501. end; {SearchSubDirs}
  1502. var
  1503. Drive: string;
  1504. P: Integer;
  1505. begin {FindNodeToPath}
  1506. Result := nil;
  1507. if Length(Path) < 3 then
  1508. Exit;
  1509. // Particularly when used by TDirView to delegate browsing to
  1510. // hidden drive view, the handle may not be created
  1511. HandleNeeded;
  1512. Drive := DriveInfo.GetDriveKey(Path);
  1513. if Assigned(GetDriveStatus(Drive).RootNode) then
  1514. begin
  1515. if DriveInfo.IsRealDrive(Drive) then
  1516. begin
  1517. System.Delete(Path, 1, 3);
  1518. end
  1519. else
  1520. if IsUncPath(Path) then
  1521. begin
  1522. System.Delete(Path, 1, 2);
  1523. P := Pos('\', Path);
  1524. if P = 0 then
  1525. begin
  1526. Path := '';
  1527. end
  1528. else
  1529. begin
  1530. System.Delete(Path, 1, P);
  1531. P := Pos('\', Path);
  1532. if P = 0 then
  1533. begin
  1534. Path := '';
  1535. end
  1536. else
  1537. begin
  1538. System.Delete(Path, 1, P);
  1539. end;
  1540. end;
  1541. end
  1542. else
  1543. begin
  1544. raise EConvertError.Create(Format(ErrorInvalidDrive, [Path]))
  1545. end;
  1546. if Length(Path) > 0 then
  1547. begin
  1548. if (not GetDriveStatus(Drive).Scanned) and (not ExistingOnly) then
  1549. begin
  1550. ScanDrive(Drive);
  1551. end;
  1552. Result := SearchSubDirs(GetDriveStatus(Drive).RootNode, UpperCase(Path));
  1553. end
  1554. else Result := GetDriveStatus(Drive).RootNode;
  1555. end;
  1556. end; {FindNodetoPath}
  1557. function TDriveView.FindNodeToPath(Path: string): TTreeNode;
  1558. begin
  1559. Result := DoFindNodeToPath(Path, False);
  1560. end;
  1561. function TDriveView.TryFindNodeToPath(Path: string): TTreeNode;
  1562. begin
  1563. Result := DoFindNodeToPath(Path, True);
  1564. end;
  1565. function TDriveView.CheckForSubDirs(Path: string): Boolean;
  1566. var
  1567. DosError: Integer;
  1568. SRec: TSearchRec;
  1569. begin
  1570. Result := False;
  1571. DosError := FindFirst(ApiPath(IncludeTrailingBackslash(Path) + '*.'), DirAttrMask, SRec);
  1572. while DosError = 0 do
  1573. begin
  1574. if (SRec.Name <> '.' ) and
  1575. (SRec.Name <> '..') and
  1576. (SRec.Attr and faDirectory <> 0) then
  1577. begin
  1578. Result := True;
  1579. Break;
  1580. end;
  1581. DosError := FindNext(SRec);
  1582. end;
  1583. FindClose(SRec);
  1584. end; {CheckForSubDirs}
  1585. function TDriveView.ReadSubDirs(Node: TTreeNode; DriveType: Integer): Boolean;
  1586. var
  1587. DosError: Integer;
  1588. SRec: TSearchRec;
  1589. NewNode: TTreeNode;
  1590. begin
  1591. Result := False;
  1592. DosError := FindFirst(ApiPath(IncludeTrailingBackslash(NodePath(Node)) + '*.*'), DirAttrMask, SRec);
  1593. while DosError = 0 do
  1594. begin
  1595. if (SRec.Name <> '.' ) and
  1596. (SRec.Name <> '..') and
  1597. (SRec.Attr and faDirectory <> 0) then
  1598. begin
  1599. NewNode := AddChildNode(Node, SRec);
  1600. if DoScanDir(NewNode) then
  1601. begin
  1602. // We have seen the SFGAO_HASSUBFOLDER to be absent on C: drive $Recycle.Bin
  1603. NewNode.HasChildren := Bool(TNodeData(NewNode.Data).shAttr and SFGAO_HASSUBFOLDER);
  1604. TNodeData(NewNode.Data).Scanned := not NewNode.HasChildren;
  1605. end
  1606. else
  1607. begin
  1608. NewNode.HasChildren := False;
  1609. TNodeData(NewNode.Data).Scanned := True;
  1610. end;
  1611. Result := True;
  1612. end;
  1613. DosError := FindNext(SRec);
  1614. end; {While DosError = 0}
  1615. FindClose(Srec);
  1616. TNodeData(Node.Data).Scanned := True;
  1617. if Result then SortChildren(Node, False)
  1618. else Node.HasChildren := False;
  1619. Application.ProcessMessages;
  1620. end; {ReadSubDirs}
  1621. function TDriveView.CallBackValidateDir(var Node: TTreeNode; Data: Pointer): Boolean;
  1622. var
  1623. WorkNode: TTreeNode;
  1624. DelNode: TTreeNode;
  1625. NewNode: TTreeNode;
  1626. SRec: TSearchRec;
  1627. SrecList: TStringList;
  1628. SubDirList: TStringList;
  1629. DosError: Integer;
  1630. Index: Integer;
  1631. NewDirFound: Boolean;
  1632. ParentDir: string;
  1633. NodeData: TNodeData;
  1634. ScanDirInfo: PScanDirInfo;
  1635. begin {CallBackValidateDir}
  1636. Result := True;
  1637. if (not Assigned(Node)) or (not Assigned(Node.Data)) then
  1638. Exit;
  1639. NewDirFound := False;
  1640. ScanDirInfo := PScanDirInfo(Data);
  1641. {Check, if directory still exists: (but not with root directory) }
  1642. if Assigned(Node.Parent) and (ScanDirInfo^.StartNode = Node) then
  1643. if not DirectoryExists(NodePathName(Node)) then
  1644. begin
  1645. WorkNode := Node.Parent;
  1646. if Selected = Node then
  1647. Selected := WorkNode;
  1648. if DropTarget = Node then
  1649. DropTarget := nil;
  1650. Node.Delete;
  1651. Node := nil;
  1652. Exit;
  1653. end;
  1654. WorkNode := Node.GetFirstChild;
  1655. NodeData := TNodeData(Node.Data);
  1656. if NodeData.Scanned and Assigned(WorkNode) then
  1657. {if node was already scanned: check wether the existing subnodes are still alive
  1658. and add all new subdirectories as subnodes:}
  1659. begin
  1660. if DoScanDir(Node) then
  1661. begin
  1662. ParentDir := IncludeTrailingBackslash(NodePath(Node));
  1663. {Build list of existing subnodes:}
  1664. SubDirList := TStringList.Create;
  1665. SubDirList.CaseSensitive := True; // We want to reflect changes in subfolder name case
  1666. while Assigned(WorkNode) do
  1667. begin
  1668. SubDirList.Add(TNodeData(WorkNode.Data).DirName);
  1669. WorkNode := Node.GetNextChild(WorkNode);
  1670. end;
  1671. // Nodes are sorted using natural sorting, while TStringList.Find uses simple sorting
  1672. SubDirList.Sort;
  1673. SRecList := TStringList.Create;
  1674. SRecList.CaseSensitive := True;
  1675. DosError := FindFirst(ApiPath(ParentDir + '*.*'), DirAttrMask, SRec);
  1676. while DosError = 0 do
  1677. begin
  1678. if (Srec.Name <> '.' ) and
  1679. (Srec.Name <> '..') and
  1680. (Srec.Attr and faDirectory <> 0) then
  1681. begin
  1682. SrecList.Add(Srec.Name);
  1683. if not SubDirList.Find(Srec.Name, Index) then
  1684. {Subnode does not exists: add it:}
  1685. begin
  1686. NewNode := AddChildNode(Node, SRec);
  1687. NewNode.HasChildren := CheckForSubDirs(ParentDir + Srec.Name);
  1688. TNodeData(NewNode.Data).Scanned := Not NewNode.HasChildren;
  1689. NewDirFound := True;
  1690. end;
  1691. end;
  1692. DosError := FindNext(Srec);
  1693. end;
  1694. FindClose(Srec);
  1695. Sreclist.Sort;
  1696. {Remove not existing subnodes:}
  1697. WorkNode := Node.GetFirstChild;
  1698. while Assigned(WorkNode) do
  1699. begin
  1700. if not Assigned(WorkNode.Data) or
  1701. not SrecList.Find(TNodeData(WorkNode.Data).DirName, Index) then
  1702. begin
  1703. DelNode := WorkNode;
  1704. WorkNode := Node.GetNextChild(WorkNode);
  1705. DelNode.Delete;
  1706. end
  1707. else
  1708. begin
  1709. if (SrecList[Index] <> TNodeData(WorkNode.Data).DirName) then
  1710. begin
  1711. {Case of directory letters has changed:}
  1712. TNodeData(WorkNode.Data).DirName := SrecList[Index];
  1713. TNodeData(WorkNode.Data).ShortName := ExtractShortPathName(NodePathName(WorkNode));
  1714. WorkNode.Text := SrecList[Index];
  1715. end;
  1716. WorkNode := Node.GetNextChild(WorkNode);
  1717. end;
  1718. end;
  1719. SrecList.Free;
  1720. SubDirList.Free;
  1721. {Sort subnodes:}
  1722. if NewDirFound then
  1723. SortChildren(Node, False);
  1724. end;
  1725. end
  1726. else
  1727. {Node was not already scanned:}
  1728. if (ScanDirInfo^.SearchNewDirs or
  1729. NodeData.Scanned or
  1730. (Node = ScanDirInfo^.StartNode)) and
  1731. DoScanDir(Node) then
  1732. begin
  1733. ReadSubDirs(Node, ScanDirInfo^.DriveType);
  1734. end;
  1735. end; {CallBackValidateDir}
  1736. procedure TDriveView.RebuildTree;
  1737. var
  1738. Drive: string;
  1739. begin
  1740. for Drive in FDriveStatus.Keys do
  1741. with GetDriveStatus(Drive) do
  1742. if Assigned(RootNode) and Scanned then
  1743. ValidateDirectory(RootNode);
  1744. end;
  1745. procedure TDriveView.ValidateCurrentDirectoryIfNotMonitoring;
  1746. begin
  1747. if Assigned(Selected) and
  1748. not Assigned(GetDriveStatus(GetDriveToNode(Selected)).DiscMonitor) then
  1749. begin
  1750. ValidateDirectory(Selected);
  1751. end;
  1752. end;
  1753. procedure TDriveView.ValidateDirectoryEx(Node: TTreeNode; Recurse: TRecursiveScan;
  1754. NewDirs: Boolean);
  1755. var
  1756. Info: PScanDirInfo;
  1757. SelDir: string;
  1758. SaveCursor: TCursor;
  1759. RestartWatchThread: Boolean;
  1760. SaveCanChange: Boolean;
  1761. CurrentPath: string;
  1762. Drive: string;
  1763. begin
  1764. if Assigned(Node) and Assigned(Node.Data) and
  1765. (not FValidateFlag) and DoScanDir(Node) then
  1766. begin
  1767. SelDir := Directory;
  1768. SaveCursor := Screen.Cursor;
  1769. if Self.Focused and (Screen.Cursor <> crHourGlass) then
  1770. Screen.Cursor := crHourGlass;
  1771. CurrentPath := NodePath(Node);
  1772. Drive := DriveInfo.GetDriveKey(CurrentPath);
  1773. if Node.Level = 0 then
  1774. GetDriveStatus(Drive).ChangeTimer.Enabled := False;
  1775. RestartWatchThread := WatchThreadActive;
  1776. try
  1777. if WatchThreadActive then
  1778. StopWatchThread;
  1779. FValidateFlag := True;
  1780. New(Info);
  1781. Info^.StartNode := Node;
  1782. Info^.SearchNewDirs := NewDirs;
  1783. Info^.DriveType := DriveInfo.Get(Drive).DriveType;
  1784. SaveCanChange := FCanChange;
  1785. FCanChange := True;
  1786. FChangeFlag := False;
  1787. Items.BeginUpdate;
  1788. try
  1789. IterateSubTree(Node, CallBackValidateDir, Recurse, coScanStartNode, Info);
  1790. finally
  1791. Items.EndUpdate;
  1792. end;
  1793. FValidateFlag := False;
  1794. if (not Assigned(Selected)) and (Length(SelDir) > 0) then
  1795. Directory := ExtractFileDrive(SelDir);
  1796. if (SelDir <> Directory) and (not FChangeFlag) then
  1797. Change(Selected);
  1798. FCanChange := SaveCanChange;
  1799. Dispose(Info);
  1800. finally
  1801. if RestartWatchThread and FWatchDirectory and not WatchThreadActive then
  1802. StartWatchThread;
  1803. if Screen.Cursor <> SaveCursor then
  1804. Screen.Cursor := SaveCursor;
  1805. end;
  1806. end;
  1807. end; {ValidateDirectoryEx}
  1808. function TDriveView.GetDriveTypeToNode(Node: TTreeNode): Integer;
  1809. begin
  1810. Assert(Assigned(Node));
  1811. Result := DriveInfo.Get(GetDriveToNode(Node)).DriveType;
  1812. end; {GetDriveTypeToNode}
  1813. procedure TDriveView.CreateWatchThread(Drive: string);
  1814. begin
  1815. if csDesigning in ComponentState then
  1816. Exit;
  1817. if (not Assigned(GetDriveStatus(Drive).DiscMonitor)) and
  1818. FWatchDirectory and
  1819. (DriveInfo.Get(Drive).DriveType <> DRIVE_REMOTE) then
  1820. begin
  1821. with GetDriveStatus(Drive) do
  1822. begin
  1823. DiscMonitor := TDiscMonitor.Create(Self);
  1824. DiscMonitor.ChangeDelay := msThreadChangeDelay;
  1825. DiscMonitor.SubTree := True;
  1826. DiscMonitor.Filters := [moDirName];
  1827. DiscMonitor.OnChange := ChangeDetected;
  1828. DiscMonitor.OnInvalid := ChangeInvalid;
  1829. DiscMonitor.SetDirectory(DriveInfo.GetDriveRoot(Drive));
  1830. DiscMonitor.Open;
  1831. end;
  1832. UpdateDriveNotifications(Drive);
  1833. end;
  1834. end; {CreateWatchThread}
  1835. procedure TDriveView.SetWatchDirectory(Value: Boolean);
  1836. begin
  1837. if FWatchDirectory <> Value then
  1838. begin
  1839. FWatchDirectory := Value;
  1840. if (not (csDesigning in ComponentState)) and Value then
  1841. StartAllWatchThreads
  1842. else
  1843. StopAllWatchThreads;
  1844. end;
  1845. end; {SetAutoScan}
  1846. procedure TDriveView.SetDirView(Value: TDirView);
  1847. begin
  1848. if Assigned(FDirView) then
  1849. FDirView.DriveView := nil;
  1850. FDirView := Value;
  1851. if Assigned(FDirView) then
  1852. FDirView.DriveView := Self;
  1853. end; {SetDirView}
  1854. function TDriveView.NodeWatched(Node: TTreeNode): Boolean;
  1855. var
  1856. Drive: string;
  1857. begin
  1858. Drive := GetDriveToNode(Node);
  1859. Result := WatchThreadActive(Drive);
  1860. end; {NodeWatched}
  1861. procedure TDriveView.ChangeInvalid(Sender: TObject; const Directory: string;
  1862. const ErrorStr: string);
  1863. var
  1864. Drive: string;
  1865. begin
  1866. Drive := DriveInfo.GetDriveKey((Sender as TDiscMonitor).Directories[0]);
  1867. with GetDriveStatus(Drive) do
  1868. begin
  1869. DiscMonitor.Close;
  1870. end;
  1871. UpdateDriveNotifications(Drive);
  1872. end; {DirWatchChangeInvalid}
  1873. procedure TDriveView.ChangeDetected(Sender: TObject; const Directory: string;
  1874. var SubdirsChanged: Boolean);
  1875. var
  1876. DirChanged: string;
  1877. begin
  1878. if Sender is TDiscMonitor then
  1879. begin
  1880. DirChanged := (Sender as TDiscMonitor).Directories[0];
  1881. if Length(DirChanged) > 0 then
  1882. begin
  1883. with GetDriveStatus(DriveInfo.GetDriveKey(DirChanged)) do
  1884. begin
  1885. ChangeTimer.Interval := 0;
  1886. ChangeTimer.Interval := FChangeInterval;
  1887. ChangeTimer.Enabled := True;
  1888. end;
  1889. end;
  1890. end;
  1891. end; {DirWatchChangeDetected}
  1892. procedure TDriveView.ChangeTimerOnTimer(Sender: TObject);
  1893. var
  1894. DriveStatusPair: TDriveStatusPair;
  1895. begin
  1896. if (FChangeTimerSuspended = 0) and (Sender is TTimer) then
  1897. begin
  1898. for DriveStatusPair in FDriveStatus do
  1899. begin
  1900. if DriveStatusPair.Value.ChangeTimer = Sender then
  1901. begin
  1902. // Messages are processed during ValidateDirectory, so we may detect another change while
  1903. // updating the directory. Prevent the recursion.
  1904. // But retry the update afterwards (by reenabling the timer in ChangeDetected)
  1905. SuspendChangeTimer;
  1906. try
  1907. with DriveStatusPair.Value.ChangeTimer do
  1908. begin
  1909. Interval := 0;
  1910. Enabled := False;
  1911. end;
  1912. if Assigned(DriveStatusPair.Value.RootNode) then
  1913. begin
  1914. {Check also collapsed (invisible) subdirectories:}
  1915. ValidateDirectory(DriveStatusPair.Value.RootNode);
  1916. end;
  1917. finally
  1918. ResumeChangeTimer;
  1919. end;
  1920. end;
  1921. end;
  1922. end;
  1923. end; {ChangeTimerOnTimer}
  1924. procedure TDriveView.UpdateDriveNotifications(Drive: string);
  1925. var
  1926. NeedNotifications: Boolean;
  1927. Path: string;
  1928. DevBroadcastHandle: DEV_BROADCAST_HANDLE;
  1929. Size: Integer;
  1930. begin
  1931. if DriveInfo.IsFixedDrive(Drive) then
  1932. begin
  1933. with GetDriveStatus(Drive) do
  1934. begin
  1935. NeedNotifications :=
  1936. WatchThreadActive(Drive) and
  1937. (DriveInfo.Get(Drive).DriveType <> DRIVE_REMOTE) and
  1938. DriveInfo.Get(Drive).DriveReady;
  1939. if NeedNotifications <> (DriveHandle <> INVALID_HANDLE_VALUE) then
  1940. begin
  1941. if NeedNotifications then
  1942. begin
  1943. Path := DriveInfo.GetDriveRoot(Drive);
  1944. DriveHandle :=
  1945. CreateFile(PChar(Path), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
  1946. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_ATTRIBUTE_NORMAL, 0);
  1947. if DriveHandle <> INVALID_HANDLE_VALUE then
  1948. begin
  1949. Size := SizeOf(DevBroadcastHandle);
  1950. ZeroMemory(@DevBroadcastHandle, Size);
  1951. DevBroadcastHandle.dbch_size := Size;
  1952. DevBroadcastHandle.dbch_devicetype := DBT_DEVTYP_HANDLE;
  1953. DevBroadcastHandle.dbch_handle := DriveHandle;
  1954. NotificationHandle :=
  1955. RegisterDeviceNotification(FInternalWindowHandle, @DevBroadcastHandle, DEVICE_NOTIFY_WINDOW_HANDLE);
  1956. if NotificationHandle = nil then
  1957. begin
  1958. CloseHandle(DriveHandle);
  1959. DriveHandle := INVALID_HANDLE_VALUE;
  1960. end;
  1961. end;
  1962. end
  1963. else
  1964. begin
  1965. UnregisterDeviceNotification(NotificationHandle);
  1966. NotificationHandle := nil;
  1967. CloseHandle(DriveHandle);
  1968. DriveHandle := INVALID_HANDLE_VALUE;
  1969. end;
  1970. end;
  1971. end;
  1972. end;
  1973. end;
  1974. procedure TDriveView.StartWatchThread;
  1975. var
  1976. Drive: string;
  1977. begin
  1978. if (csDesigning in ComponentState) or
  1979. not Assigned(Selected) or
  1980. not fWatchDirectory then Exit;
  1981. Drive := GetDriveToNode(Selected);
  1982. with GetDriveStatus(Drive) do
  1983. begin
  1984. if not Assigned(DiscMonitor) then
  1985. CreateWatchThread(Drive);
  1986. if Assigned(DiscMonitor) and not DiscMonitor.Enabled then
  1987. DiscMonitor.Enabled := True;
  1988. end;
  1989. UpdateDriveNotifications(Drive);
  1990. end; {StartWatchThread}
  1991. procedure TDriveView.StopWatchThread;
  1992. var
  1993. Drive: string;
  1994. begin
  1995. if Assigned(Selected) then
  1996. begin
  1997. Drive := GetDriveToNode(Selected);
  1998. with GetDriveStatus(Drive) do
  1999. if Assigned(DiscMonitor) then
  2000. DiscMonitor.Enabled := False;
  2001. UpdateDriveNotifications(Drive);
  2002. end;
  2003. end; {StopWatchThread}
  2004. procedure TDriveView.SuspendChangeTimer;
  2005. begin
  2006. Inc(FChangeTimerSuspended);
  2007. end;
  2008. procedure TDriveView.ResumeChangeTimer;
  2009. begin
  2010. Assert(FChangeTimerSuspended > 0);
  2011. Dec(FChangeTimerSuspended);
  2012. end;
  2013. procedure TDriveView.TerminateWatchThread(Drive: string);
  2014. begin
  2015. with GetDriveStatus(Drive) do
  2016. if Assigned(DiscMonitor) then
  2017. begin
  2018. DiscMonitor.Free;
  2019. DiscMonitor := nil;
  2020. end;
  2021. UpdateDriveNotifications(Drive);
  2022. end; {StopWatchThread}
  2023. procedure TDriveView.StartAllWatchThreads;
  2024. var
  2025. DriveStatusPair: TDriveStatusPair;
  2026. Drive: string;
  2027. begin
  2028. if (csDesigning in ComponentState) or (not FWatchDirectory) then
  2029. Exit;
  2030. for DriveStatusPair in FDriveStatus do
  2031. with DriveStatusPair.Value do
  2032. if Scanned then
  2033. begin
  2034. if not Assigned(DiscMonitor) then
  2035. CreateWatchThread(DriveStatusPair.Key);
  2036. if Assigned(DiscMonitor) and (not DiscMonitor.Active) then
  2037. begin
  2038. DiscMonitor.Open;
  2039. UpdateDriveNotifications(DriveStatusPair.Key);
  2040. end;
  2041. end;
  2042. if Assigned(Selected) then
  2043. begin
  2044. Drive := GetDriveToNode(Selected);
  2045. if not DriveInfo.IsFixedDrive(Drive) then
  2046. begin
  2047. StartWatchThread;
  2048. end;
  2049. end;
  2050. end; {StartAllWatchThreads}
  2051. procedure TDriveView.StopAllWatchThreads;
  2052. var
  2053. DriveStatusPair: TDriveStatusPair;
  2054. begin
  2055. if (csDesigning in ComponentState) or (not FWatchDirectory) then
  2056. Exit;
  2057. for DriveStatusPair in FDriveStatus do
  2058. with DriveStatusPair.Value do
  2059. begin
  2060. if Assigned(DiscMonitor) then
  2061. begin
  2062. DiscMonitor.Close;
  2063. UpdateDriveNotifications(DriveStatusPair.Key);
  2064. end;
  2065. end;
  2066. end; {StopAllWatchThreads}
  2067. function TDriveView.WatchThreadActive(Drive: string): Boolean;
  2068. begin
  2069. Result := FWatchDirectory and
  2070. Assigned(GetDriveStatus(Drive).DiscMonitor) and
  2071. GetDriveStatus(Drive).DiscMonitor.Active and
  2072. GetDriveStatus(Drive).DiscMonitor.Enabled;
  2073. end; {WatchThreadActive}
  2074. function TDriveView.WatchThreadActive: Boolean;
  2075. var
  2076. Drive: string;
  2077. begin
  2078. if not Assigned(Selected) then
  2079. begin
  2080. Result := False;
  2081. Exit;
  2082. end;
  2083. Drive := GetDriveToNode(Selected);
  2084. Result := WatchThreadActive(Drive);
  2085. end; {WatchThreadActive}
  2086. procedure TDriveView.SetFullDriveScan(DoFullDriveScan: Boolean);
  2087. begin
  2088. FFullDriveScan := DoFullDriveScan;
  2089. end; {SetAutoScan}
  2090. function TDriveView.FindPathNode(Path: string): TTreeNode;
  2091. var
  2092. PossiblyHiddenPath: string;
  2093. Attrs: Integer;
  2094. begin
  2095. if Assigned(FOnNeedHiddenDirectories) and
  2096. (not ShowHiddenDirs) and
  2097. DirectoryExistsFix(Path) then // do not even bother if the path does not exist
  2098. begin
  2099. PossiblyHiddenPath := ExcludeTrailingPathDelimiter(Path);
  2100. while (PossiblyHiddenPath <> '') and
  2101. (not IsRootPath(PossiblyHiddenPath)) do // Drives have hidden attribute
  2102. begin
  2103. Attrs := FileGetAttr(PossiblyHiddenPath, False);
  2104. if (Attrs and faHidden) = faHidden then
  2105. begin
  2106. if Assigned(FOnNeedHiddenDirectories) then
  2107. begin
  2108. FOnNeedHiddenDirectories(Self);
  2109. end;
  2110. Break;
  2111. end
  2112. else
  2113. begin
  2114. PossiblyHiddenPath := ExtractFileDir(PossiblyHiddenPath);
  2115. end;
  2116. end;
  2117. end;
  2118. {Find existing path or parent path of not existing path:}
  2119. repeat
  2120. Result := FindNodeToPath(Path);
  2121. if not Assigned(Result) then
  2122. Path := ExtractFilePath(ExcludeTrailingBackslash(Path));
  2123. until Assigned(Result) or (Length(Path) < 3);
  2124. end;
  2125. procedure TDriveView.SetDirectory(Value: string);
  2126. begin
  2127. Value := IncludeTrailingBackslash(Value);
  2128. FDirectory := Value;
  2129. inherited;
  2130. if Assigned(Selected) and (Selected.Level = 0) then
  2131. begin
  2132. if not GetDriveStatus(GetDriveToNode(Selected)).Scanned then
  2133. ScanDrive(GetDriveToNode(Selected));
  2134. end;
  2135. end; {SetDirectory}
  2136. function TDriveView.GetDirName(Node: TTreeNode): string;
  2137. begin
  2138. if Assigned(Node) and Assigned(Node.Data) then
  2139. Result := TNodeData(Node.Data).DirName
  2140. else
  2141. Result := '';
  2142. end; {GetDirName}
  2143. {GetDrive: returns the drive of the Node.}
  2144. function TDriveView.GetDriveToNode(Node: TTreeNode): string;
  2145. var
  2146. Path: string;
  2147. begin
  2148. if (not Assigned (Node)) or (not Assigned(Node.Data)) then
  2149. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDrive']));
  2150. Path := NodePath(Node);
  2151. Result := DriveInfo.GetDriveKey(Path);
  2152. end; {GetDrive}
  2153. {RootNode: returns the rootnode to the Node:}
  2154. function TDriveView.RootNode(Node: TTreeNode): TTreeNode;
  2155. begin
  2156. Result := Node;
  2157. if not Assigned(Node) then
  2158. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['RootNode']));
  2159. while Assigned(Result.Parent) do
  2160. Result := Result.Parent;
  2161. end; {RootNode}
  2162. function TDriveView.GetDisplayName(Node: TTreeNode): string;
  2163. begin
  2164. Result := '';
  2165. if (not Assigned(Node)) or (not Assigned(Node.Data)) then
  2166. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDisplayName']));
  2167. if Node.Level = 0 then Result := GetDriveText(GetDriveToNode(Node))
  2168. else
  2169. begin
  2170. Result := GetDirName(Node);
  2171. end;
  2172. end; {GetDisplayName}
  2173. procedure TDriveView.SetShowVolLabel(ShowIt: Boolean);
  2174. begin
  2175. if ShowIt = FShowVolLabel then
  2176. Exit;
  2177. FShowVolLabel := ShowIt;
  2178. RefreshRootNodes(dvdsFloppy);
  2179. end; {SetShowVolLabel}
  2180. procedure TDriveView.DisplayContextMenu(Node: TTreeNode; Point: TPoint);
  2181. var
  2182. Verb: string;
  2183. DirWatched: Boolean;
  2184. begin
  2185. DirWatched := NodeWatched(Node) and WatchThreadActive;
  2186. Assert(Node <> nil);
  2187. if Node <> Selected then
  2188. DropTarget := Node;
  2189. Verb := EmptyStr;
  2190. if Assigned(FOnDisplayContextMenu) then
  2191. FOnDisplayContextMenu(Self);
  2192. ShellDisplayContextMenu(FParentForm.Handle, Point, NodePathName(Node),
  2193. CanEdit(Node), Verb, False);
  2194. if Verb = shcRename then Node.EditText
  2195. else
  2196. if Verb = shcCut then
  2197. begin
  2198. LastClipBoardOperation := cboCut;
  2199. LastPathCut := NodePathName(Node);
  2200. end
  2201. else
  2202. if Verb = shcCopy then LastClipBoardOperation := cboCopy
  2203. else
  2204. if Verb = shcPaste then
  2205. PasteFromClipBoard(NodePathName(Node));
  2206. DropTarget := nil;
  2207. if not DirWatched then
  2208. ValidateDirectory(Node);
  2209. end; {DisplayContextMenu (2)}
  2210. procedure TDriveView.DisplayPropertiesMenu(Node: TTreeNode);
  2211. begin
  2212. Assert(Assigned(Node));
  2213. ShellExecuteContextCommand(FParentForm.Handle, shcProperties, NodePathName(Node));
  2214. end; {ContextMenu}
  2215. procedure TDriveView.SetSelected(Node: TTreeNode);
  2216. begin
  2217. if Node <> Selected then
  2218. begin
  2219. FChangeFlag := False;
  2220. FCanChange := True;
  2221. inherited Selected := Node;
  2222. if not FChangeFlag then
  2223. Change(Selected);
  2224. end;
  2225. end; {SetSelected}
  2226. {Called by TFileDeleteThread, when a file deletion was detected by the D&D receiving application:}
  2227. procedure TDriveView.SignalDirDelete(Sender: TObject; Files: TStringList);
  2228. begin
  2229. if Files.Count > 0 then
  2230. ValidateDirectory(FindNodeToPath(Files[0]));
  2231. end; {SignalDirDelete}
  2232. function TDriveView.DDSourceEffects: TDropEffectSet;
  2233. begin
  2234. if FDragNode.Level = 0 then
  2235. Result := [deLink]
  2236. else
  2237. Result := [deLink, deCopy, deMove];
  2238. end;
  2239. procedure TDriveView.DDChooseEffect(KeyState: Integer; var Effect: Integer; PreferredEffect: Integer);
  2240. begin
  2241. if DropTarget = nil then Effect := DROPEFFECT_NONE
  2242. else
  2243. if (KeyState and (MK_CONTROL or MK_SHIFT) = 0) and (PreferredEffect = 0) then
  2244. begin
  2245. if FDragDrive <> '' then
  2246. begin
  2247. if FExeDrag and DriveInfo.IsFixedDrive(GetDriveToNode(DropTarget)) and DriveInfo.IsFixedDrive(FDragDrive) then
  2248. begin
  2249. Effect := DROPEFFECT_LINK;
  2250. end
  2251. else
  2252. if (Effect = DROPEFFECT_COPY) and
  2253. (SameText(FDragDrive, GetDriveToNode(DropTarget)) and
  2254. (FDragDropFilesEx.AvailableDropEffects and DROPEFFECT_MOVE <> 0)) then
  2255. begin
  2256. Effect := DROPEFFECT_MOVE;
  2257. end;
  2258. end;
  2259. end;
  2260. inherited;
  2261. end;
  2262. function TDriveView.DragCompleteFileList: Boolean;
  2263. begin
  2264. Result := (GetDriveTypeToNode(FDragNode) <> DRIVE_REMOVABLE);
  2265. end;
  2266. function TDriveView.DDExecute: TDragResult;
  2267. var
  2268. WatchThreadOK: Boolean;
  2269. DragParentPath: string;
  2270. DragPath: string;
  2271. begin
  2272. WatchThreadOK := WatchThreadActive;
  2273. Result := FDragDropFilesEx.Execute(nil);
  2274. if (Result = drMove) and (not WatchThreadOK) then
  2275. begin
  2276. DragPath := NodePathName(FDragNode);
  2277. if Assigned(FDragNode.Parent) then
  2278. DragParentPath := NodePathName(FDragNode.Parent)
  2279. else
  2280. DragParentPath := DragPath;
  2281. if (FDragNode.Level > 0) or (DragParentPath <> NodePathName(Selected.Parent)) then
  2282. begin
  2283. FDragNode := FindNodeToPath(DragPath);
  2284. if Assigned(FDragNode) then
  2285. begin
  2286. FDragFileList.Clear;
  2287. FDragFileList.Add(DragPath);
  2288. TFileDeleteThread.Create(FDragFileList, MaxWaitTimeOut, SignalDirDelete);
  2289. end;
  2290. end;
  2291. end;
  2292. end;
  2293. procedure TDriveView.PerformDragDropFileOperation(Node: TTreeNode; Effect: Integer);
  2294. var
  2295. Index: Integer;
  2296. SourcePath: string;
  2297. SourceParentPath: string;
  2298. SourceIsDirectory: Boolean;
  2299. SaveCursor: TCursor;
  2300. SourceNode, TargetNode: TTreeNode;
  2301. TargetPath: string;
  2302. IsRecycleBin: Boolean;
  2303. begin
  2304. TargetPath := NodePathName(Node);
  2305. IsRecycleBin := NodeIsRecycleBin(Node);
  2306. if FDragDropFilesEx.FileList.Count = 0 then
  2307. Exit;
  2308. SaveCursor := Screen.Cursor;
  2309. Screen.Cursor := crHourGlass;
  2310. SourcePath := EmptyStr;
  2311. try
  2312. if (Effect = DROPEFFECT_COPY) or (Effect = DROPEFFECT_MOVE) then
  2313. begin
  2314. StopAllWatchThreads;
  2315. if Assigned(FDirView) then
  2316. FDirView.StopWatchThread;
  2317. if Assigned(DropSourceControl) and
  2318. (DropSourceControl is TDirView) and
  2319. (DropSourceControl <> FDirView) then
  2320. begin
  2321. TDirView(DropSourceControl).StopWatchThread;
  2322. end;
  2323. if DropFiles(
  2324. DragDropFilesEx, Effect, FFileOperator, TargetPath, false, IsRecycleBin, ConfirmDelete, ConfirmOverwrite, False,
  2325. Self, OnDDFileOperation, SourcePath, SourceIsDirectory) then
  2326. begin
  2327. if Assigned(FOnDDFileOperationExecuted) then
  2328. FOnDDFileOperationExecuted(Self, Effect, SourcePath, TargetPath);
  2329. end;
  2330. ClearDragFileList(FDragDropFilesEx.FileList);
  2331. // TDirView.PerformDragDropFileOperation validates the SourcePath and that actually seems correct
  2332. SourceParentPath := ExtractFilePath(ExcludeTrailingBackslash(SourcePath));
  2333. end
  2334. else
  2335. if Effect = DROPEFFECT_LINK then
  2336. { Create Link requested: }
  2337. begin
  2338. for Index := 0 to FDragDropFilesEx.FileList.Count - 1 do
  2339. begin
  2340. if not DropLink(PFDDListItem(FDragDropFilesEx.FileList[Index]), TargetPath) then
  2341. begin
  2342. DDError(DDCreateShortCutError);
  2343. end;
  2344. end;
  2345. end;
  2346. if Effect = DROPEFFECT_MOVE then
  2347. Items.BeginUpdate;
  2348. {Update source directory, if move-operation was performed:}
  2349. if ((Effect = DROPEFFECT_MOVE) or IsRecycleBin) then
  2350. begin
  2351. // See comment in corresponding operation in TDirView.PerformDragDropFileOperation
  2352. SourceNode := TryFindNodeToPath(SourceParentPath);
  2353. if Assigned(SourceNode) then
  2354. ValidateDirectory(SourceNode);
  2355. end;
  2356. {Update subdirectories of target directory:}
  2357. TargetNode := FindNodeToPath(TargetPath);
  2358. if Assigned(TargetNode) then
  2359. ValidateDirectory(TargetNode)
  2360. else
  2361. ValidateDirectory(GetDriveStatus(DriveInfo.GetDriveKey(TargetPath)).RootNode);
  2362. if Effect = DROPEFFECT_MOVE then
  2363. Items.EndUpdate;
  2364. {Update linked component TDirView:}
  2365. if Assigned(FDirView) and (not FDirView.WatchThreadActive) then
  2366. begin
  2367. case Effect of
  2368. DROPEFFECT_COPY,
  2369. DROPEFFECT_LINK:
  2370. if (IncludeTrailingBackslash(TargetPath) = IncludeTrailingBackslash(DirView.Path)) then
  2371. FDirView.Reload2;
  2372. DROPEFFECT_MOVE:
  2373. if (IncludeTrailingBackslash(TargetPath) = IncludeTrailingBackslash(DirView.Path)) or
  2374. (IncludeTrailingBackslash(SourceParentPath) = IncludeTrailingBackslash(DirView.Path)) then
  2375. begin
  2376. if FDirView <> DropSourceControl then FDirView.Reload2;
  2377. end;
  2378. end; {Case}
  2379. end;
  2380. {Update the DropSource control, if files are moved and it is a TDirView:}
  2381. if (Effect = DROPEFFECT_MOVE) and (DropSourceControl is TDirView) then
  2382. begin
  2383. TDirView(DropSourceControl).ValidateSelectedFiles;
  2384. end;
  2385. finally
  2386. FFileOperator.OperandFrom.Clear;
  2387. FFileOperator.OperandTo.Clear;
  2388. StartAllWatchThreads;
  2389. if Assigned(FDirView) and (not FDirView.WatchThreadActive) then
  2390. FDirView.StartWatchThread;
  2391. if Assigned(DropSourceControl) and (DropSourceControl is TDirView) and
  2392. (not TDirView(DropSourceControl).WatchThreadActive) then
  2393. TDirView(DropSourceControl).StartWatchThread;
  2394. Screen.Cursor := SaveCursor;
  2395. end;
  2396. end; {PerformDragDropFileOperation}
  2397. function TDriveView.GetCanUndoCopyMove: Boolean;
  2398. begin
  2399. Result := Assigned(FFileOperator) and FFileOperator.CanUndo;
  2400. end; {CanUndoCopyMove}
  2401. function TDriveView.UndoCopyMove: Boolean;
  2402. var
  2403. LastTarget: string;
  2404. LastSource: string;
  2405. begin
  2406. Result := False;
  2407. if FFileOperator.CanUndo then
  2408. begin
  2409. Lasttarget := FFileOperator.LastOperandTo[0];
  2410. LastSource := FFileOperator.LastOperandFrom[0];
  2411. StopAllWatchThreads;
  2412. Result := FFileOperator.UndoExecute;
  2413. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastTarget)));
  2414. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastSource)));
  2415. StartAllWatchThreads;
  2416. if Assigned(FDirView) then
  2417. with FDirView do
  2418. if not WatchThreadActive then
  2419. begin
  2420. if (IncludeTrailingBackslash(ExtractFilePath(LastTarget)) = IncludeTrailingBackslash(Path)) or
  2421. (IncludeTrailingBackslash(ExtractFilePath(LastSource)) = IncludeTrailingBackslash(Path)) then
  2422. Reload2;
  2423. end;
  2424. end;
  2425. end; {UndoCopyMove}
  2426. {Clipboard operations:}
  2427. procedure TDriveView.SetLastPathCut(Path: string);
  2428. var
  2429. Node: TTreeNode;
  2430. begin
  2431. if FLastPathCut <> Path then
  2432. begin
  2433. Node := FindNodeToPath(FLastPathCut);
  2434. if Assigned(Node) then
  2435. begin
  2436. FLastPathCut := Path;
  2437. Node.Cut := False;
  2438. end;
  2439. Node := FindNodeToPath(Path);
  2440. if Assigned(Node) then
  2441. begin
  2442. FLastPathCut := Path;
  2443. Node.Cut := True;
  2444. end;
  2445. end;
  2446. end; {SetLastNodeCut}
  2447. procedure TDriveView.EmptyClipboard;
  2448. begin
  2449. if Windows.OpenClipBoard(0) then
  2450. begin
  2451. Windows.EmptyClipBoard;
  2452. Windows.CloseClipBoard;
  2453. LastPathCut := '';
  2454. LastClipBoardOperation := cboNone;
  2455. if Assigned(FDirView) then
  2456. FDirView.EmptyClipboard;
  2457. end;
  2458. end; {EmptyClipBoard}
  2459. function TDriveView.CopyToClipBoard(Node: TTreeNode): Boolean;
  2460. begin
  2461. Result := Assigned(Selected);
  2462. if Result then
  2463. begin
  2464. EmptyClipBoard;
  2465. ClearDragFileList(FDragDropFilesEx.FileList);
  2466. AddToDragFileList(FDragDropFilesEx.FileList, Selected);
  2467. Result := FDragDropFilesEx.CopyToClipBoard;
  2468. LastClipBoardOperation := cboCopy;
  2469. end;
  2470. end; {CopyToClipBoard}
  2471. function TDriveView.CutToClipBoard(Node: TTreeNode): Boolean;
  2472. begin
  2473. Result := Assigned(Node) and (Node.Level > 0) and CopyToClipBoard(Node);
  2474. if Result then
  2475. begin
  2476. LastPathCut := NodePathName(Node);
  2477. LastClipBoardOperation := cboCut;
  2478. end;
  2479. end; {CutToClipBoard}
  2480. function TDriveView.CanPasteFromClipBoard: Boolean;
  2481. begin
  2482. Result := False;
  2483. if Assigned(Selected) and Windows.OpenClipboard(0) then
  2484. begin
  2485. Result := IsClipboardFormatAvailable(CF_HDROP);
  2486. Windows.CloseClipBoard;
  2487. end;
  2488. end; {CanPasteFromClipBoard}
  2489. function TDriveView.PasteFromClipBoard(TargetPath: String = ''): Boolean;
  2490. begin
  2491. ClearDragFileList(FDragDropFilesEx.FileList);
  2492. Result := False;
  2493. if CanPasteFromClipBoard and {MP}FDragDropFilesEx.GetFromClipBoard{/MP}
  2494. then
  2495. begin
  2496. if TargetPath = '' then
  2497. TargetPath := NodePathName(Selected);
  2498. case LastClipBoardOperation of
  2499. cboCopy,
  2500. cboNone:
  2501. begin
  2502. PerformDragDropFileOperation(Selected, DROPEFFECT_COPY);
  2503. if Assigned(FOnDDExecuted) then
  2504. FOnDDExecuted(Self, DROPEFFECT_COPY);
  2505. end;
  2506. cboCut:
  2507. begin
  2508. PerformDragDropFileOperation(Selected, DROPEFFECT_MOVE);
  2509. if Assigned(FOnDDExecuted) then
  2510. FOnDDExecuted(Self, DROPEFFECT_MOVE);
  2511. EmptyClipBoard;
  2512. end;
  2513. end;
  2514. Result := True;
  2515. end;
  2516. end; {PasteFromClipBoard}
  2517. end.