NeoMutt  2021-02-05-666-ge300cd
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  /* Any new/deleted mailboxes will cause a refresh. As long as
90  * they're valid, our pointers will be updated in prepare_sidebar() */
91 
92  struct IndexSharedData *shared = wdata->shared;
93  struct SbEntry *entry = mutt_mem_calloc(1, sizeof(struct SbEntry));
94  entry->mailbox = m;
95 
96  if (wdata->top_index < 0)
97  wdata->top_index = ARRAY_SIZE(&wdata->entries);
98  if (wdata->bot_index < 0)
99  wdata->bot_index = ARRAY_SIZE(&wdata->entries);
100  if ((wdata->opn_index < 0) && shared->mailbox &&
101  mutt_str_equal(m->realpath, shared->mailbox->realpath))
102  {
103  wdata->opn_index = ARRAY_SIZE(&wdata->entries);
104  }
105 
106  ARRAY_ADD(&wdata->entries, entry);
107 }
108 
114 void sb_remove_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
115 {
116  struct SbEntry **sbep = NULL;
117  ARRAY_FOREACH(sbep, &wdata->entries)
118  {
119  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
120  {
121  struct SbEntry *sbe_remove = *sbep;
122  ARRAY_REMOVE(&wdata->entries, sbep);
123  FREE(&sbe_remove);
124 
125  if (wdata->opn_index == ARRAY_FOREACH_IDX)
126  {
127  // Open item was deleted
128  wdata->opn_index = -1;
129  }
130  else if ((wdata->opn_index > 0) && (wdata->opn_index > ARRAY_FOREACH_IDX))
131  {
132  // Open item is still visible, so adjust the index
133  wdata->opn_index--;
134  }
135 
136  if (wdata->hil_index == ARRAY_FOREACH_IDX)
137  {
138  // If possible, keep the highlight where it is
139  struct SbEntry **sbep_cur = ARRAY_GET(&wdata->entries, ARRAY_FOREACH_IDX);
140  if (!sbep_cur)
141  {
142  // The last entry was deleted, so backtrack
143  select_prev(wdata);
144  }
145  else if ((*sbep)->is_hidden)
146  {
147  // Find the next unhidden entry, or the previous
148  if (!select_next(wdata))
149  if (!select_prev(wdata))
150  wdata->hil_index = -1;
151  }
152  }
153  else if ((wdata->hil_index > 0) && (wdata->hil_index > ARRAY_FOREACH_IDX))
154  {
155  // Highlighted item is still visible, so adjust the index
156  wdata->hil_index--;
157  }
158  break;
159  }
160  }
161 }
162 
168 void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
169 {
170  wdata->opn_index = -1;
171 
172  struct SbEntry **sbep = NULL;
173  ARRAY_FOREACH(sbep, &wdata->entries)
174  {
175  if (m)
176  {
177  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
178  {
179  wdata->opn_index = ARRAY_FOREACH_IDX;
180  wdata->hil_index = ARRAY_FOREACH_IDX;
181  break;
182  }
183  }
184  (*sbep)->is_hidden = ((*sbep)->mailbox->flags & MB_HIDDEN);
185  }
186 }
187 
191 void sb_init(void)
192 {
193  COMMANDS_REGISTER(sb_commands);
194 
195  // Listen for dialog creation events
198 }
199 
203 void sb_shutdown(void)
204 {
205  if (AllDialogsWindow)
208 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
int opn_index
Current (open) mailbox.
Definition: private.h:68
int top_index
First mailbox visible in sidebar.
Definition: private.h:67
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:206
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:105
Data shared between Index, Pager and Sidebar.
Definition: shared_data.h:36
Sidebar private Window data -.
Definition: private.h:62
Info about folders in the sidebar.
Definition: private.h:39
int hil_index
Highlighted mailbox.
Definition: private.h:69
Container for Accounts, Notifications.
Definition: neomutt.h:36
#define COMMANDS_REGISTER(cmds)
Definition: mutt_commands.h:82
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
Convenience wrapper for the config headers.
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:44
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Convenience wrapper for the core headers.
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
#define MB_HIDDEN
Definition: mailbox.h:38
A mailbox.
Definition: mailbox.h:81
Definitions of NeoMutt commands.
struct MuttWindow * AllDialogsWindow
Parent of all Dialogs.
Definition: dialog.c:76
enum CommandResult sb_parse_whitelist(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;sidebar_whitelist&#39; command - Implements Command::parse() -.
Definition: commands.c:42
#define ARRAY_REMOVE(head, elem)
Remove an entry from the array, shifting down the subsequent entries.
Definition: array.h:261
#define FREE(x)
Definition: memory.h:40
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:152
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
enum CommandResult sb_parse_unwhitelist(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the &#39;unsidebar_whitelist&#39; command - Implements Command::parse() -.
Definition: commands.c:61
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:41
Convenience wrapper for the library headers.
int bot_index
Last mailbox visible in sidebar.
Definition: private.h:70
GUI manage the main index (list of emails)
struct IndexSharedData * shared
Definition: private.h:64
void * wdata
Private data.
Definition: mutt_window.h:145
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:83