1414typedef struct FileViewTaskRec_ {
1515 wchar_t path [1024 ];
1616 LCUI_BOOL active ;
17+ LCUI_BOOL has_error ;
1718 LCUI_Widget folders ;
1819 LCUI_Widget files ;
20+ size_t count ;
1921} FileViewTaskRec , * FileViewTask ;
2022
2123typedef struct FileViewRec_ {
@@ -66,7 +68,7 @@ static LCUI_Widget CreateFileItem(const wchar_t *dirname,
6668 1023 - dirname_len , ENCODING_ANSI );
6769 path [dirname_len + len ] = 0 ;
6870 if (stat (path , & file_stat ) == 0 ) {
69- strftime (date_str , 64 , "%F %T" , localtime (& file_stat .st_ctime ));
71+ strftime (date_str , 64 , "%F %T" , localtime (& file_stat .st_mtime ));
7072 TextView_SetText (date , date_str );
7173
7274 unit_i = 0 ;
@@ -94,6 +96,7 @@ static LCUI_Widget CreateFileItem(const wchar_t *dirname,
9496 }
9597 full_path [2047 ] = 0 ;
9698 Widget_SetAttribute (link , "to" , full_path );
99+ Widget_AddClass (item , "is-folder" );
97100 } else {
98101 Icon_SetName (icon , "file-outline" );
99102 link = LCUIWidget_New ("text" );
@@ -115,17 +118,38 @@ static LCUI_Widget CreateFileItem(const wchar_t *dirname,
115118 return item ;
116119}
117120
118- static void FileView_OnLinkFiles (void * arg1 , void * arg2 )
121+ static void FileView_OnLoadedFiles (void * arg1 , void * arg2 )
119122{
123+ LCUI_Widget message ;
124+ LCUI_WidgetEventRec e ;
125+
120126 FileViewTask task = arg2 ;
121127 FileView self = Widget_GetData (arg1 , file_proto );
122128
123129 if (task -> active ) {
124- Widget_Append (self -> body , task -> folders );
125- Widget_Append (self -> body , task -> files );
126- Widget_Unwrap (task -> folders );
127- Widget_Unwrap (task -> files );
130+ if (task -> has_error ) {
131+ message = LCUIWidget_New ("text" );
132+ TextView_SetTextW (message ,
133+ L"[b][color=#f00]Error:[/color][/b] cannot "
134+ L"read the files in this directory." );
135+ Widget_Append (self -> body -> parent , message );
136+ } else if (task -> count < 1 ) {
137+ message = LCUIWidget_New ("text" );
138+ TextView_SetTextW (message , L"This directory is empty." );
139+ Widget_Append (self -> body -> parent , message );
140+ } else {
141+ Widget_Append (self -> body , task -> folders );
142+ Widget_Append (self -> body , task -> files );
143+ Widget_Unwrap (task -> folders );
144+ Widget_Unwrap (task -> files );
145+ }
146+ LCUI_InitWidgetEvent (& e , "PageLoaded" );
147+ Widget_TriggerEvent (arg1 , & e , NULL );
148+ } else {
149+ Widget_Destroy (task -> files );
150+ Widget_Destroy (task -> folders );
128151 }
152+ self -> task = NULL ;
129153 free (task );
130154}
131155
@@ -142,9 +166,11 @@ static void FileView_OnLoadFiles(void *arg1, void *arg2)
142166 free (t );
143167 return ;
144168 }
145- if (LCUI_OpenDirW (t -> path , & dir ) != 0 ) {
146- return ;
147- }
169+ if (LCUI_OpenDirW (t -> path , & dir ) != 0 ) {
170+ t -> has_error = TRUE;
171+ LCUI_PostSimpleTask (FileView_OnLoadedFiles , arg1 , t );
172+ return ;
173+ }
148174 t -> folders = LCUIWidget_New (NULL );
149175 t -> files = LCUIWidget_New (NULL );
150176 while ((entry = LCUI_ReadDirW (& dir ))) {
@@ -157,6 +183,7 @@ static void FileView_OnLoadFiles(void *arg1, void *arg2)
157183 continue ;
158184 }
159185 }
186+ t -> count ++ ;
160187 if (LCUI_FileIsDirectory (entry )) {
161188 Widget_Append (t -> folders ,
162189 CreateFileItem (t -> path , name , TRUE));
@@ -170,7 +197,7 @@ static void FileView_OnLoadFiles(void *arg1, void *arg2)
170197 free (t );
171198 return ;
172199 }
173- LCUI_PostSimpleTask (FileView_OnLinkFiles , arg1 , t );
200+ LCUI_PostSimpleTask (FileView_OnLoadedFiles , arg1 , t );
174201}
175202
176203static void FileView_OnReady (LCUI_Widget w , LCUI_WidgetEvent e , void * arg )
@@ -201,14 +228,16 @@ static void FileView_OnReady(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
201228 router = router_get_by_name (name );
202229 route = router_get_current_route (router );
203230 self -> task = malloc (sizeof (FileViewTaskRec ));
231+ self -> task -> count = 0 ;
204232 self -> task -> active = TRUE;
233+ self -> task -> has_error = FALSE;
205234 self -> header = Dict_FetchValue (refs , "header" );
206235 self -> body = Dict_FetchValue (refs , "body" );
207236 path = router_route_get_param (route , "pathMatch" );
208237 if (path ) {
209238 LCUI_DecodeUTF8String (self -> task -> path , path , 1023 );
210239 self -> task -> path [1023 ] = 0 ;
211- swprintf (header_str , 1023 , L"Directory listing for %s" ,
240+ swprintf (header_str , 1023 , L"Directory listing for / %s" ,
212241 self -> task -> path );
213242 header_str [1023 ] = 0 ;
214243 } else {
@@ -217,6 +246,7 @@ static void FileView_OnReady(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
217246 wcscpy (header_str , L"Directory listing for /" );
218247 }
219248 TextView_SetTextW (self -> header , header_str );
249+ Widget_SetTitleW (w , header_str );
220250 task .arg [0 ] = w ;
221251 task .arg [1 ] = self -> task ;
222252 task .destroy_arg [0 ] = NULL ;
0 commit comments