Blender V4.3
folder_history.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#include <cstring>
12
13#include "BLI_listbase.h"
14#include "BLI_path_utils.hh"
15#include "BLI_string.h"
16
17#include "BKE_context.hh"
18
19#include "DNA_space_types.h"
20
21#include "ED_fileselect.hh"
22
23#include "MEM_guardedalloc.h"
24
25#include "file_intern.hh"
26
27/* -------------------------------------------------------------------- */
35
36void folderlist_popdir(ListBase *folderlist, char *dir)
37{
38 const char *prev_dir;
39 FolderList *folder;
40 folder = static_cast<FolderList *>(folderlist->last);
41
42 if (folder) {
43 /* remove the current directory */
44 MEM_freeN(folder->foldername);
45 BLI_freelinkN(folderlist, folder);
46
47 folder = static_cast<FolderList *>(folderlist->last);
48 if (folder) {
49 prev_dir = folder->foldername;
50 BLI_strncpy(dir, prev_dir, FILE_MAXDIR);
51 }
52 }
53 /* Delete the folder next or use set-directory directly before PREVIOUS OP. */
54}
55
56void folderlist_pushdir(ListBase *folderlist, const char *dir)
57{
58 if (!dir[0]) {
59 return;
60 }
61
62 FolderList *folder, *previous_folder;
63 previous_folder = static_cast<FolderList *>(folderlist->last);
64
65 /* check if already exists */
66 if (previous_folder && previous_folder->foldername) {
67 if (BLI_path_cmp(previous_folder->foldername, dir) == 0) {
68 return;
69 }
70 }
71
72 /* create next folder element */
73 folder = MEM_cnew<FolderList>(__func__);
74 folder->foldername = BLI_strdup(dir);
75
76 /* add it to the end of the list */
77 BLI_addtail(folderlist, folder);
78}
79
80const char *folderlist_peeklastdir(ListBase *folderlist)
81{
82 FolderList *folder;
83
84 if (!folderlist->last) {
85 return nullptr;
86 }
87
88 folder = static_cast<FolderList *>(folderlist->last);
89 return folder->foldername;
90}
91
93{
95 FolderList *folder;
96
97 /* if there is no folder_next there is nothing we can clear */
99 return false;
100 }
101
102 /* if previous_folder, next_folder or refresh_folder operators are executed
103 * it doesn't clear folder_next */
104 folder = static_cast<FolderList *>(sfile->folders_prev->last);
105 if ((!folder) || (BLI_path_cmp(folder->foldername, params->dir) == 0)) {
106 return false;
107 }
108
109 /* eventually clear flist->folders_next */
110 return true;
111}
112
113void folderlist_free(ListBase *folderlist)
114{
115 if (folderlist) {
116 LISTBASE_FOREACH_MUTABLE (FolderList *, folder, folderlist) {
117 MEM_freeN(folder->foldername);
118 MEM_delete(folder);
119 }
120 BLI_listbase_clear(folderlist);
121 }
122}
123
125{
126 ListBase folderlistn = {nullptr};
127
128 BLI_duplicatelist(&folderlistn, folderlist);
129
130 LISTBASE_FOREACH (FolderList *, folder, &folderlistn) {
131 folder->foldername = (char *)MEM_dupallocN(folder->foldername);
132 }
133 return folderlistn;
134}
135
138/* -------------------------------------------------------------------- */
143{
145 if (history->browse_mode == browse_mode) {
146 return history;
147 }
148 }
149
150 return nullptr;
151}
152
154{
156
157 if (!history) {
158 history = MEM_cnew<FileFolderHistory>(__func__);
159 history->browse_mode = sfile->browse_mode;
160 BLI_addtail(&sfile->folder_histories, history);
161 }
162
163 sfile->folders_next = &history->folders_next;
164 sfile->folders_prev = &history->folders_prev;
165}
166
168{
169 if (sfile->folders_prev == &history->folders_prev) {
170 sfile->folders_prev = nullptr;
171 }
172 if (sfile->folders_next == &history->folders_next) {
173 sfile->folders_next = nullptr;
174 }
175 folderlist_free(&history->folders_prev);
176 folderlist_free(&history->folders_next);
177 BLI_freelinkN(&sfile->folder_histories, history);
178}
179
181{
183 folder_history_entry_free(sfile, history);
184 }
185}
186
188{
189 ListBase histories = {nullptr};
190
191 LISTBASE_FOREACH (FileFolderHistory *, history, listbase) {
192 FileFolderHistory *history_new = static_cast<FileFolderHistory *>(MEM_dupallocN(history));
193 history_new->folders_prev = folderlist_duplicate(&history->folders_prev);
194 history_new->folders_next = folderlist_duplicate(&history->folders_next);
195 BLI_addtail(&histories, history_new);
196 }
197
198 return histories;
199}
200
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
#define LISTBASE_FOREACH(type, var, list)
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:269
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
void void void void void void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
#define FILE_MAXDIR
#define BLI_path_cmp
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.c:40
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
eFileBrowse_Mode
FileSelectParams * ED_fileselect_get_active_params(const SpaceFile *sfile)
Definition filesel.cc:380
Read Guarded memory(de)allocation.
bool folderlist_clear_next(SpaceFile *sfile)
const char * folderlist_peeklastdir(ListBase *folderlist)
void folder_history_list_ensure_for_active_browse_mode(SpaceFile *sfile)
void folder_history_list_free(SpaceFile *sfile)
void folderlist_pushdir(ListBase *folderlist, const char *dir)
ListBase folder_history_list_duplicate(ListBase *listbase)
void folderlist_free(ListBase *folderlist)
static FileFolderHistory * folder_history_find(const SpaceFile *sfile, eFileBrowse_Mode browse_mode)
static ListBase folderlist_duplicate(ListBase *folderlist)
void folderlist_popdir(ListBase *folderlist, char *dir)
static void folder_history_entry_free(SpaceFile *sfile, FileFolderHistory *history)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
FolderList * next
char * foldername
FolderList * prev
void * last
ListBase * folders_prev
ListBase * folders_next
ListBase folder_histories