~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 50a526b4935e2ff0e37aa53f47a71417517ddefa CTU-FEE-B0B35APO-Semestral-project/file-browser/src/window_browser.c -rw-r--r-- 11.0 KiB
50a526b4 — František Boháček feat: add size and date to browser table 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "window_browser.h"
#include "dialog.h"
#include "display_utils.h"
#include "file_access.h"
#include "file_open.h"
#include "font.h"
#include "gui.h"
#include "gui_component_text.h"
#include "gui_container_info.h"
#include "gui_list_commands.h"
#include "gui_component_line.h"
#include "gui_window_info.h"
#include "input.h"
#include "logger.h"
#include "path.h"
#include "renderer.h"
#include <stdio.h>
#include <time.h>

#define COLUMNS_COUNT 4
#define MAX_COLUMN_CHARS 200

char *column_names[] ={"NAME", "TYPE", "SIZE", "MODIFIED"};

typedef struct {
  bool running;
  gui_t *gui;

  container_t *list_container;
  component_t *line_component;
  window_t *browser_window;

  font_t *font;

  gui_list_command_state_t click_state;
  text_t text_state;

  directory_t *current_directory;
  fileaccess_state_t state;

  uint16_t column_widths[COLUMNS_COUNT];
} browser_window_state_t;

static bool browser_window_list_render_item(void *state, uint32_t index,
                                            renderer_t *renderer, int16_t beg_x,
                                            int16_t beg_y,
                                            display_pixel_t color);

static bool browser_window_list_render_header(void *state, uint32_t index,
                                              renderer_t *renderer,
                                              int16_t beg_x, int16_t beg_y,
                                              display_pixel_t color);

static void browser_window_item_clicked(container_t *container, void *state,
                                        uint32_t selected_index);

static void *browser_window_construct(window_t *window, void *state);
static bool browser_window_running(void *state);
static void browser_window_job(void *state);

static char *browser_get_column_data(file_t *file, uint16_t column, char* out);

gui_container_info_t window_browser_containers[] = {
    {.type = CONT_TABLE,
     .payload.list = {.render_item_fn = browser_window_list_render_item,
                      .render_header_fn = browser_window_list_render_header,
                      .item_height = 16}},
    {.type = CONT_GROUP, .payload.group.components_count = 2},
};

window_info_t window_browser_info = {
    .construct = browser_window_construct,
    .containers_count = 2,
    .containers = window_browser_containers,
};

bool window_browser_open_local(gui_t *gui, font_t *font) {
  fileaccess_state_t state = fileaccess_init(&local_file_access, NULL);
  return window_browser_open(gui, font, state);
}

bool window_browser_open(gui_t *gui, font_t *font, fileaccess_state_t state) {
  directory_or_error_t root = fileaccess_root_list(state);

  if (root.error) {
    fileaccess_log_error(gui->logger, root.payload.error);
    dialog_info_show(gui, font, "Could not open root directory",
                     fileaccess_get_error_text(root.payload.error));
    return false;
  }

  browser_window_state_t bstate = {
      .state = state,
      .gui = gui,
      .font = font,
      .current_directory = root.payload.directory,
      .running = true,
  };

  uint16_t commands_state = commands_save_state(gui->commands);
  gui_window_init_and_loop(gui, &bstate, window_browser_info, browser_window_running,
                           browser_window_job);
  commands_restore_state(gui->commands, commands_state);

  return true;
}

static void command_handler_exit(void *state, int amount) {
  browser_window_state_t *bstate = (browser_window_state_t *)state;
  if (bstate->gui->active_window == bstate->browser_window) {
    bstate->running = false;
  }
}

static void *browser_window_construct(window_t *window, void *state) {
  browser_window_state_t *bstate = (browser_window_state_t *)state;
  logger_t *logger = bstate->gui->logger;
  logger_info(logger, __FILE__, __FUNCTION__, __LINE__,
              "Constructing browser window");
  bstate->list_container = &window->containers[0];
  bstate->browser_window = window;

  bstate->click_state.container = bstate->list_container;
  bstate->click_state.state = state;
  bstate->click_state.clicked = browser_window_item_clicked;
  bstate->click_state.font = bstate->font;
  bstate->click_state.gui = bstate->gui;
  bstate->click_state.window = window;

  bstate->text_state.font = bstate->font;
  bstate->text_state.line = bstate->current_directory->path;
  bstate->text_state.color = WHITE_PIXEL;

  // containers init
  // group components init
  component_t path_text = gui_text_create(&bstate->text_state, 3, 3, bstate->gui->size.x, 0);
  component_t line_component = gui_line_create(&WHITE_PIXEL, 0, path_text.height + path_text.y + 3, 1000, 1);
  
  gui_group_container_add_component(&window->containers[1], path_text);
  bstate->line_component =
      gui_group_container_add_component(&window->containers[1], line_component);

  // list init
  gui_container_info_init(bstate->list_container, bstate,
                          bstate->current_directory->files_count, 5,
                          bstate->font->size / 2 + 3);
  bstate->list_container->width = bstate->gui->size.x - 20;
  bstate->list_container->height = bstate->gui->size.y - bstate->list_container->y - 20;

  // commands register
  gui_list_commands_register(bstate->gui->commands, &bstate->click_state);
  commands_register(bstate->gui->commands, IN_KEYBOARD, 'e',
                    command_handler_exit, state);

  return state;
}

static void browser_window_item_clicked(container_t *container, void *state,
                                        uint32_t selected_index) {

  browser_window_state_t *bstate = (browser_window_state_t *)state;
  logger_t *logger = bstate->gui->logger;

  file_t current_file = bstate->current_directory->files[selected_index];

  if (current_file.type == FT_FILE) {
    // open
    logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Opening file %s",
                current_file.name);
    file_operation_error_t error = file_open(&current_file, browser_exec_options, bstate->state);
    if (error != FILOPER_SUCCESS) {
      fileaccess_log_error(logger, error);
      dialog_info_show(bstate->gui, bstate->font, "Could not open file", fileaccess_get_error_text(error));
    } else {
      logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Successfully returned from executing file.");
    }
  } else if (current_file.type == FT_FOLDER || current_file.type == FT_OTHER) {
    char new_dir_path[path_join_memory_size(bstate->current_directory->path, current_file.name)];
    path_join(bstate->current_directory->path, current_file.name, new_dir_path);

    directory_or_error_t data = fileaccess_directory_list(bstate->state, new_dir_path);
    if (data.error) {
      fileaccess_log_error(logger, data.payload.error);
      dialog_info_show(bstate->gui, bstate->font, "Could not open directory",
                       fileaccess_get_error_text(data.payload.error));
    } else {
      fileaccess_directory_close(bstate->state, bstate->current_directory);
      bstate->current_directory = data.payload.directory;
      bstate->text_state.line = bstate->current_directory->path;

      gui_list_container_set_state(bstate->list_container, bstate, bstate->current_directory->files_count);

      logger_info(logger, __FILE__, __FUNCTION__, __LINE__, "Opening directory %s", bstate->current_directory->path);
    }
  }
}

static bool browser_window_list_render_item(void *state, uint32_t index,
                                            renderer_t *renderer, int16_t beg_x,
                                            int16_t beg_y,
                                            display_pixel_t color) {
  browser_window_state_t *bstate = (browser_window_state_t *)state;
  logger_t *logger = bstate->gui->logger;
  if (index >= bstate->current_directory->files_count) {
    logger_error(logger, __FILE__, __FUNCTION__, __LINE__, "Tried to reach item out of index");
    return false;
  }
  file_t file = bstate->current_directory->files[index];

  uint16_t offset = beg_x;
  char tmp[MAX_COLUMN_CHARS];
  for (int i = 0; i < COLUMNS_COUNT; i++) {
    char *data = browser_get_column_data(&file, i, tmp);
    renderer_write_string(renderer, offset, beg_y, 0, bstate->font, data,
                          color);
    offset += bstate->column_widths[i];
  }

  return true;
}

static bool browser_window_list_render_header(void *state, uint32_t index,
                                              renderer_t *renderer,
                                              int16_t beg_x, int16_t beg_y,
                                              display_pixel_t color) {
  browser_window_state_t *bstate = (browser_window_state_t *)state;
  renderer_render_rectangle(renderer, beg_x - 3, beg_y + bstate->font->size,
                            10000, 1, color);

  uint16_t offset = beg_x;

  for (int i = 0; i < COLUMNS_COUNT; i++) {
    renderer_write_string(renderer, offset, beg_y, 0, bstate->font, column_names[i], color);
    offset += bstate->column_widths[i];
  }
  return true;
}

static bool browser_window_running(void *state) {
  browser_window_state_t *bstate = (browser_window_state_t*)state;
  return bstate->running;
}

static void browser_window_job(void *state) {
  browser_window_state_t *bstate = (browser_window_state_t *)state;

  char tmp[MAX_COLUMN_CHARS];
  for (int i = 0; i < COLUMNS_COUNT; i++) {
    uint16_t max_size = font_measure_text(bstate->font, column_names[i]).x;
    for (int j = 0; j < bstate->current_directory->files_count; j++) {
      char *data = browser_get_column_data(&bstate->current_directory->files[j], i, tmp);
      if (data == NULL) {
        continue;
      }
      uint16_t current_size = font_measure_text(bstate->font, data).x;

      if (current_size > max_size) {
        max_size = current_size;
      }
    }
    bstate->column_widths[i] = max_size + 50;
  }

  bstate->line_component->y = bstate->font->size + 5;
  bstate->list_container->y = bstate->line_component->y / 2;
  gui_list_container_set_item_height(bstate->list_container,
                                     bstate->font->size);

  if (!bstate->running) {
    // cleanup
    fileaccess_directory_close(bstate->state, bstate->current_directory);
  }
}

#define KiB 1024LU
#define MiB KiB*KiB
#define GiB KiB*KiB*KiB
#define TiB KiB*KiB*KiB*KiB

static char *browser_get_column_data(file_t *file, uint16_t column, char *out) {
  switch (column) {
  case 0:
    return file->name;
  case 1:
    switch (file->type) {
    case FT_FILE:
      return "FILE";
    case FT_FOLDER:
      return "DIR";
    case FT_OTHER:
      return "OTHER";
    case FT_UNKNOWN:
      return "UNKNOWN";
    }
    break;
  case 2:
    // get size
    {
      uint64_t size = file->size;
      double transformed = size;
      char *append = "B";

      if (size > TiB) {
        transformed /= TiB;
        append = "TiB";
      } else if (size > GiB) {
        transformed /= GiB;
        append = "GiB";
      } else if (size > MiB) {
        transformed /= MiB;
        append = "MiB";
      } else if (size > KiB) {
        transformed /= KiB;
        append = "KiB";
      }
      
      sprintf(out, "%.2f %s", transformed, append);
      return out;
    }
  case 3:
    // date modified

    strftime(out, MAX_COLUMN_CHARS, "%c", localtime(&file->modify_time));
    return out;
  }

  return NULL;
}
Do not follow this link