NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
sidebar.c
Go to the documentation of this file.
1 
32 #include "config.h"
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include "private.h"
36 #include "mutt/lib.h"
37 #include "config/lib.h"
38 #include "core/lib.h"
39 #include "gui/lib.h"
40 #include "lib.h"
41 #include "index/lib.h"
42 #include "mutt_commands.h"
43 
45 
46 static const struct Command sb_commands[] = {
47  // clang-format off
48  { "sidebar_whitelist", sb_parse_whitelist, 0 },
49  { "unsidebar_whitelist", sb_parse_unwhitelist, 0 },
50  // clang-format on
51 };
52 
58 struct Mailbox *sb_get_highlight(struct MuttWindow *win)
59 {
60  const bool c_sidebar_visible =
61  cs_subset_bool(NeoMutt->sub, "sidebar_visible");
62  if (!c_sidebar_visible)
63  return NULL;
64 
65  struct SidebarWindowData *wdata = sb_wdata_get(win);
66  if (wdata->hil_index < 0)
67  return NULL;
68 
69  struct SbEntry **sbep = ARRAY_GET(&wdata->entries, wdata->hil_index);
70  if (!sbep)
71  return NULL;
72 
73  return (*sbep)->mailbox;
74 }
75 
84 void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
85 {
86  if (!m)
87  return;
88 
89  struct SbEntry **sbep = NULL;
90  ARRAY_FOREACH(sbep, &wdata->entries)
91  {
92  if ((*sbep)->mailbox == m)
93  return;
94  }
95 
96  /* Any new/deleted mailboxes will cause a refresh. As long as
97  * they're valid, our pointers will be updated in prepare_sidebar() */
98 
99  struct IndexSharedData *shared = wdata->shared;
100  struct SbEntry *entry = mutt_mem_calloc(1, sizeof(struct SbEntry));
101  entry->mailbox = m;
102 
103  if (wdata->top_index < 0)
104  wdata->top_index = ARRAY_SIZE(&wdata->entries);
105  if (wdata->bot_index < 0)
106  wdata->bot_index = ARRAY_SIZE(&wdata->entries);
107  if ((wdata->opn_index < 0) && shared->mailbox &&
108  mutt_str_equal(m->realpath, shared->mailbox->realpath))
109  {
110  wdata->opn_index = ARRAY_SIZE(&wdata->entries);
111  }
112 
113  ARRAY_ADD(&wdata->entries, entry);
114 }
115 
121 void sb_remove_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
122 {
123  struct SbEntry **sbep = NULL;
124  ARRAY_FOREACH(sbep, &wdata->entries)
125  {
126  if ((*sbep)->mailbox != m)
127  continue;
128 
129  struct SbEntry *sbe_remove = *sbep;
130  ARRAY_REMOVE(&wdata->entries, sbep);
131  FREE(&sbe_remove);
132 
133  if (wdata->opn_index == ARRAY_FOREACH_IDX)
134  {
135  // Open item was deleted
136  wdata->opn_index = -1;
137  }
138  else if ((wdata->opn_index > 0) && (wdata->opn_index > ARRAY_FOREACH_IDX))
139  {
140  // Open item is still visible, so adjust the index
141  wdata->opn_index--;
142  }
143 
144  if (wdata->hil_index == ARRAY_FOREACH_IDX)
145  {
146  // If possible, keep the highlight where it is
147  struct SbEntry **sbep_cur = ARRAY_GET(&wdata->entries, ARRAY_FOREACH_IDX);
148  if (!sbep_cur)
149  {
150  // The last entry was deleted, so backtrack
151  select_prev(wdata);
152  }
153  else if ((*sbep)->is_hidden)
154  {
155  // Find the next unhidden entry, or the previous
156  if (!select_next(wdata))
157  if (!select_prev(wdata))
158  wdata->hil_index = -1;
159  }
160  }
161  else if ((wdata->hil_index > 0) && (wdata->hil_index > ARRAY_FOREACH_IDX))
162  {
163  // Highlighted item is still visible, so adjust the index
164  wdata->hil_index--;
165  }
166  break;
167  }
168 }
169 
175 void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
176 {
177  wdata->opn_index = -1;
178 
179  struct SbEntry **sbep = NULL;
180  ARRAY_FOREACH(sbep, &wdata->entries)
181  {
182  if (m)
183  {
184  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
185  {
186  wdata->opn_index = ARRAY_FOREACH_IDX;
187  wdata->hil_index = ARRAY_FOREACH_IDX;
188  break;
189  }
190  }
191  (*sbep)->is_hidden = ((*sbep)->mailbox->flags & MB_HIDDEN);
192  }
193 }
194 
198 void sb_init(void)
199 {
201 
202  // Listen for dialog creation events
205 }
206 
210 void sb_shutdown(void)
211 {
212  if (AllDialogsWindow)
215 }
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:152
#define ARRAY_REMOVE(head, elem)
Remove an entry from the array, shifting down the subsequent entries.
Definition: array.h:263
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:208
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:83
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:105
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
struct MuttWindow * AllDialogsWindow
Parent of all Dialogs.
Definition: dialog.c:74
enum CommandResult sb_parse_whitelist(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'sidebar_whitelist' command - Implements Command::parse() -.
Definition: commands.c:42
enum CommandResult sb_parse_unwhitelist(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'unsidebar_whitelist' command - Implements Command::parse() -.
Definition: commands.c:61
int sb_insertion_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: observer.c:470
Convenience wrapper for the gui headers.
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
GUI manage the main index (list of emails)
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
#define MB_HIDDEN
Definition: mailbox.h:38
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:40
Convenience wrapper for the library headers.
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:715
Definitions of NeoMutt commands.
#define COMMANDS_REGISTER(cmds)
Definition: mutt_commands.h:47
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
bool select_prev(struct SidebarWindowData *wdata)
Selects the previous unhidden mailbox.
Definition: functions.c:113
bool select_next(struct SidebarWindowData *wdata)
Selects the next unhidden mailbox.
Definition: functions.c:45
GUI display the mailboxes in a side panel.
struct SidebarWindowData * sb_wdata_get(struct MuttWindow *win)
Get the Sidebar data for this window.
Definition: wdata.c:70
void sb_init(void)
Set up the Sidebar.
Definition: sidebar.c:198
static const struct Command sb_commands[]
Definition: sidebar.c:46
void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Set the current Mailbox.
Definition: sidebar.c:175
struct Mailbox * sb_get_highlight(struct MuttWindow *win)
Get the Mailbox that's highlighted in the sidebar.
Definition: sidebar.c:58
struct ListHead SidebarWhitelist
List of mailboxes to always display in the sidebar.
Definition: sidebar.c:44
void sb_shutdown(void)
Clean up the Sidebar.
Definition: sidebar.c:210
void sb_remove_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Remove a Mailbox from the Sidebar.
Definition: sidebar.c:121
void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
Add a Mailbox to the Sidebar.
Definition: sidebar.c:84
Key value store.
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:37
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:41
A mailbox.
Definition: mailbox.h:82
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Info about folders in the sidebar.
Definition: private.h:41
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:45
Sidebar private Window data -.
Definition: private.h:65
int top_index
First mailbox visible in sidebar.
Definition: private.h:69
int bot_index
Last mailbox visible in sidebar.
Definition: private.h:72
int hil_index
Highlighted mailbox.
Definition: private.h:71
struct IndexSharedData * shared
Shared Index Data.
Definition: private.h:66
int opn_index
Current (open) mailbox.
Definition: private.h:70
struct SbEntryArray entries
Items to display in the sidebar.
Definition: private.h:67