~~Title: Reading a Directory~~
Perhaps the most important function in the plugin API is the [[:reference:plugins:vfs_plugins:vfs_readdirectory|VFS_ReadDirectory]] function. This is called by Opus whenever the user browses to a new folder in your plugin's namespace.
BOOL VFS_ReadDirectory( HANDLE hVFSData, LPVFSFUNCDATA lpFuncData,
LPVFSREADDIRDATA lpReadDirData )
The **hVFSData** parameter is your instance handle, returned from [[:reference:plugins:vfs_plugins:vfs_create|VFS_Create]]. This parameter is passed to almost all of the functions in the plugin. The **lpFuncData** parameter is a general purpose data pointer that currently is not used. Many functions accept this parameter and it may be used in future revisions of the API to provide additional information or callback parameters to your functions. For now you should ignore this parameter - it will usually be NULL.
The final parameter, **lpReadDirData**, is a pointer to a **VFSREADDIRDATA** structure. This structure provides information about the folder to be read, the type of event that has triggered the read, and provides a mechanism for you to pass back to Opus information about the folder contents.
The members of the **VFSREADDIRDATA** structure are as follows:
* **UINT cbSize** \\ Directory Opus initializes this field to the size of the structure before passing it to your function. You must only use fields in the structure up to the size supplied by Directory Opus. This allows your plugin to be compatible with future versions of Opus that may support more fields in this structure.
* **HWND hwndParent** \\ This field contains a window handle that can be considered the "parent" window for the purposes of displaying any status dialogs, error dialogs or any other user interaction required by your plugin.
* **LPTSTR lpszPath** \\ This field is a pointer to a string containing the full path of the folder that Directory Opus wants to read the contents of.
* **vfsReadType vfsReadOp** \\ This field contains an enumeration value that indicates the operation, or reason for the directory read. Several values of this enumeration have special meaning and must be observed - many others may be ignored unless your plugin has special behavior in these situations. Note that several codes do not actually indicate directory read functions, and so you should not return any directory information for these operations.
* **VFSREAD_CHANGEDIR** \\ This indicates that this is a "change directory" operation only. You should not actually return the contents of the directory - just use this as a hint to perform any directory change operation that your plugin may require.
* **VFSREAD_NORMAL** \\ This indicates a normal directory read operation.
* **VFSREAD_REFRESH** \\ The current folder is being refreshed. For all intents and purposes this is a normal directory read operation and you are free to treat it as such - a separate operation code is provided merely for information purposes.
* **VFSREAD_PARENT** \\ The directory read is being performed because a **Go UP** function has been executed. Note that the **lpszPath** field already contains the parent folder - you do not need to calculate the parent path yourself. As with **VFSREAD_REFRESH**, you are free to treat this as a normal directory read operation unless your plugin requires special processing for a parent function.
* **VFSREAD_ROOT** \\ In the same way as **VFSREAD_PARENT**, this code indicates the directory is being read because a **Go ROOT** function has been executed.
* **VFSREAD_BACK** \\ In the same way as the previous two operation codes, this code indicates the directory is being read because of a **Go BACK** function.
* **VFSREAD_FORWARD** \\ And again, this code indicates the directory is being read because of a **Go FORWARD** function.
* **VFSREAD_PRINTDIR** \\ The directory is being read by the **Print Directory** function.
* **VFSREAD_FREEDIR** / **VFSREAD_FREEDIRCLOSE** \\ These codes indicates a "free directory" operation only. You should not actually return the contents of the directory. If you have any cached data you may treat this code as a hint to free it. The VFSREAD_FREEDIR code indicates that the current directory is being freed but the namespace may be re-used to read another subsequent directory. The VFSREAD_FREEDIRCLOSE code indicates that the Lister (or file display) itself is being closed, and so the namespace will not be used again. If you do not have any processing you need to perform you are free to ignore this code altogether.
* **HANDLE hAbortEvent** \\ This may be a handle to a Windows **event** object which is used to abort the reading of a directory if, for example, the user clicks the Abort button or closes the Lister while the directory read is taking place. If you wish to allow the user to abort your plugin (which is highly recommended), you should make sure that you check the event for //signaled// status at regular intervals. For example,
if ( hAbortEvent && WaitForSingleObject( hAbortEvent, 0 ) == WAIT_OBJECT_0 )
{
// aborted
}
* **HANDLE hMemHeap** \\ This is a handle to a memory heap created with the Windows **HeapCreate** function. You will use this heap with the **HeapAlloc** function to allocate memory needed to return the directory contents.
* **LPVFSFILEDATAHEADER lpFileData** \\ This field is initially set to 0 when your [[:reference:plugins:vfs_plugins:vfs_readdirectory|VFS_ReadDirectory]] function is called. For those operations that require you to return directory information (all except **VFSREAD_CHANGEDIR** and **VFSREAD_FREEDIR**), you use this field to return the actual directory contents. \\ \\ The **VFSFILEDATAHEADER** structure is the header directly preceding in memory an arbitrarily sized array of **VFSFILEDATA** structures. The **VFSFILEDATAHEADER** contains an optional pointer to the next header in a linked list - so you are free to return the directory contents as either one single memory allocation or as a linked list of multiple allocations, each containing an arbitrary number of entries. \\ \\ The basic structure in memory looks like this:
lpFileData
|
v
+-------------------+
| VFSFILEDATAHEADER |---+
+-------------------+ |
| VFSFILEDATA | |
| VFSFILEDATA | | lpNext
| VFSFILEDATA | |
+-------------------+ |
|
+-------------------+ |
| VFSFILEDATAHEADER |<--+
+-------------------+
| VFSFILEDATA |
| VFSFILEDATA |
+-------------------+
The **lpFileData** member of the structure must be set to point to the address of the first allocated **VFSFILEDATAHEADER** structure. The **lpNext** member of the **VFSFILEDATAHEADER** structure is used to chain on to subsequent allocation blocks. Set the **lpNext** member of the final allocation to 0.
Note that all memory must be allocated using the Windows **HeapAlloc** function and the memory heap passed in **hMemHeap**. Opus will free the memory you allocate using **HeapFree** and/or **HeapDestroy** when it no longer needs it.
The members of the **VFSFILEDATAHEADER** structure are:
* **UINT cbSize** \\ This field must be set to the size of the **VFSFILEDATAHEADER** structure you are using, ie sizeof(VFSFILEDATAHEADER). It is not the total allocation size. Directory Opus uses this value to find the first of the **VFSFILEDATA** structures, which follow the header structure in memory.
* **LPVFSFILEDATAHEADER lpNext** \\ Points to the next allocated chunk of file data, or set to 0 if there are no further chunks.
* **int iNumItems** \\ Set this to the number of records (each one of which is a **VFSFILEDATA** structure) included in this data chunk.
* **UINT cbFileDataSize** \\ Set this to the size of the **VFSFILEDATA** structure you are using, ie sizeof(VFSFILEDATA). Directory Opus uses this value to iterate through the supplied file records.
Immediately following the **VFSFILEDATAHEADER** structure in memory must be one or more **VFSFILEDATA** structures, the number of which is given by the **iNumItems** member in the header structure.
The members of the **VFSFILEDATA** structure are:
* **WIN32_FIND_DATA wfdData** \\ This is a standard Windows API **WIN32_FIND_DATA** structure as used by the **FindFirst** / **FindNext** functions. You must initialize all appropriate fields in this structure to represent the directory item you are adding. See the MSDN docs for information on this structure.
* **DWORD dwFlags** \\ This is a flags field - although no flags are currently used with the [[:reference:plugins:vfs_plugins:vfs_readdirectory|VFS_ReadDirectory]] function, and so this must be set to 0 for now.
* **LPTSTR lpszComment** \\ This field points to an optional comment string for the item you are adding. If provided this will be displayed to the user in the Comment or Description fields in the Lister. The string this field points to must be allocated using **HeapAlloc** with the memory heap passed in **hMemHeap** (the same way the **VFSFILEDATAHEADER** etc structure was allocated.) Set to 0 if no comment is to be provided.
* **int iNumColumns** \\ This member indicates the number of custom columns for which data is to be provided for this directory item. The **lpvfsColumnData** member points to an array of this number of **VFSFILEDATACOLUMN** structures. Set to 0 if you are not using custom columns or not providing any custom column data for this item.
* **LPVFSFILEDATACOLUMN lpvfsColumnData** \\ This member points to an array of **VFSFILEDATACOLUMN** structures. This array must be allocated using **HeapAlloc** with the memory heap passed in **hMemHeap**. The size of the array is given by the **iNumColumns** field. The **VFSFILEDATACOLUMN** structure has two members - **iColumnId** specifies the custom column ID (the same value specified in the column definitions returned by the [[:reference:plugins:vfs_plugins:vfs_getcustomcolumns|VFS_GetCustomColumns]] function), and **lpszValue**, which points to a null-terminated string containing the column data. If you are not using custom columns set this member to 0.
Once you have allocated and returned the directory contents, Opus will display the folder in the Lister. By default, icons displayed for your files and folders are the default system ones (that is, the standard folder icon for your folders, and the appropriate icon for your files based on their filename extensions).
If you wish to provide custom icons for your files and folders you should implement the [[:reference:plugins:vfs_plugins:vfs_getfileicon|VFS_GetFileIcon]] function. This function will be called by Opus for every file and folder in your plugin's namespace (but only when the icon is actually required, and potentially on a background thread.)