NeoMutt  2020-09-25
Teaching an old dog new tricks
DOXYGEN
sidebar.c
Go to the documentation of this file.
1 
32 #include "config.h"
33 #include <stdio.h>
34 #include "private.h"
35 #include "mutt/lib.h"
36 #include "core/lib.h"
37 #include "gui/lib.h"
38 #include "lib.h"
39 #include "context.h"
40 #include "mutt_globals.h"
41 
43 
44 const struct Command sb_commands[] = {
45  // clang-format off
46  { "sidebar_whitelist", sb_parse_whitelist, 0 },
47  { "unsidebar_whitelist", sb_parse_unwhitelist, 0 },
48  // clang-format on
49 };
50 
56 struct Mailbox *sb_get_highlight(struct MuttWindow *win)
57 {
58  if (!C_SidebarVisible)
59  return NULL;
60 
61  struct SidebarWindowData *wdata = sb_wdata_get(win);
62  if (wdata->hil_index < 0)
63  return NULL;
64 
65  struct SbEntry **sbep = ARRAY_GET(&wdata->entries, wdata->hil_index);
66  if (!sbep)
67  return NULL;
68 
69  return (*sbep)->mailbox;
70 }
71 
80 void sb_add_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
81 {
82  static int seq_unsorted = 1;
83 
84  if (!m)
85  return;
86 
87  /* Any new/deleted mailboxes will cause a refresh. As long as
88  * they're valid, our pointers will be updated in prepare_sidebar() */
89 
90  struct SbEntry *entry = mutt_mem_calloc(1, sizeof(struct SbEntry));
91  entry->mailbox = m;
92  entry->seq_unsorted = seq_unsorted++;
93 
94  if (wdata->top_index < 0)
95  wdata->top_index = ARRAY_SIZE(&wdata->entries);
96  if (wdata->bot_index < 0)
97  wdata->bot_index = ARRAY_SIZE(&wdata->entries);
98  if ((wdata->opn_index < 0) && Context &&
100  {
101  wdata->opn_index = ARRAY_SIZE(&wdata->entries);
102  }
103 
104  ARRAY_ADD(&wdata->entries, entry);
105 }
106 
112 void sb_remove_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
113 {
114  struct SbEntry **sbep = NULL;
115  ARRAY_FOREACH(sbep, &wdata->entries)
116  {
117  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
118  {
119  struct SbEntry *sbe_remove = *sbep;
120  ARRAY_REMOVE(&wdata->entries, sbep);
121  FREE(&sbe_remove);
122 
123  if (wdata->opn_index == ARRAY_FOREACH_IDX)
124  {
125  // Open item was deleted
126  wdata->opn_index = -1;
127  }
128  else if ((wdata->opn_index > 0) && (wdata->opn_index > ARRAY_FOREACH_IDX))
129  {
130  // Open item is still visible, so adjust the index
131  wdata->opn_index--;
132  }
133 
134  if (wdata->hil_index == ARRAY_FOREACH_IDX)
135  {
136  // If possible, keep the highlight where it is
137  struct SbEntry **sbep_cur = ARRAY_GET(&wdata->entries, ARRAY_FOREACH_IDX);
138  if (!sbep_cur)
139  {
140  // The last entry was deleted, so backtrack
141  select_prev(wdata);
142  }
143  else if ((*sbep)->is_hidden)
144  {
145  // Find the next unhidden entry, or the previous
146  if (!select_next(wdata))
147  if (!select_prev(wdata))
148  wdata->hil_index = -1;
149  }
150  }
151  else if ((wdata->hil_index > 0) && (wdata->hil_index > ARRAY_FOREACH_IDX))
152  {
153  // Highlighted item is still visible, so adjust the index
154  wdata->hil_index--;
155  }
156  break;
157  }
158  }
159 }
160 
166 void sb_set_current_mailbox(struct SidebarWindowData *wdata, struct Mailbox *m)
167 {
168  wdata->opn_index = -1;
169 
170  struct SbEntry **sbep = NULL;
171  ARRAY_FOREACH(sbep, &wdata->entries)
172  {
173  if (m)
174  {
175  if (mutt_str_equal((*sbep)->mailbox->realpath, m->realpath))
176  {
177  wdata->opn_index = ARRAY_FOREACH_IDX;
178  wdata->hil_index = ARRAY_FOREACH_IDX;
179  break;
180  }
181  }
182  (*sbep)->is_hidden = ((*sbep)->mailbox->flags & MB_HIDDEN);
183  }
184 }
185 
189 void sb_init(void)
190 {
191  COMMANDS_REGISTER(sb_commands);
192 
193  // Listen for dialog creation events
195 }
196 
200 void sb_shutdown(void)
201 {
202  if (AllDialogsWindow)
205 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
Convenience wrapper for the gui headers.
The "current" mailbox.
Definition: context.h:38
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
int opn_index
Current (open) mailbox.
Definition: private.h:67
int top_index
First mailbox visible in sidebar.
Definition: private.h:66
The "currently-open" mailbox.
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
Sidebar private Window data -.
Definition: private.h:62
A user-callable command.
Definition: mutt_commands.h:45
A division of the screen.
Definition: mutt_window.h:115
Info about folders in the sidebar.
Definition: private.h:38
int hil_index
Highlighted mailbox.
Definition: private.h:68
#define COMMANDS_REGISTER(cmds)
Definition: mutt_commands.h:77
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:42
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:43
struct Notify * notify
Notifications system.
Definition: mutt_window.h:131
struct Mailbox * mailbox
Definition: context.h:50
int sb_insertion_observer(struct NotifyCallback *nc)
Listen for new Dialogs - Implements observer_t.
Definition: observer.c:429
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:169
#define MB_HIDDEN
Definition: mailbox.h:38
A mailbox.
Definition: mailbox.h:81
struct MuttWindow * AllDialogsWindow
Parent of all Dialogs.
Definition: mutt_window.c:46
int seq_unsorted
Sequence number of unsorted Mailbox list.
Definition: private.h:46
#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:321
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:207
Hundreds of global variables to back the user variables.
Convenience wrapper for the library headers.
int bot_index
Last mailbox visible in sidebar.
Definition: private.h:69
void * wdata
Private data.
Definition: mutt_window.h:138
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:83