NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
attach.c
Go to the documentation of this file.
1 
62 #include "config.h"
63 #include <stddef.h>
64 #include <stdbool.h>
65 #include <stdint.h>
66 #include "private.h"
67 #include "mutt/lib.h"
68 #include "config/lib.h"
69 #include "email/lib.h"
70 #include "core/lib.h"
71 #include "gui/lib.h"
72 #include "menu/lib.h"
73 #include "send/lib.h"
74 #include "attach_data.h"
75 #include "format_flags.h"
76 #include "muttlib.h"
77 #include "recvattach.h"
78 #include "shared_data.h"
79 
89 unsigned long cum_attachs_size(struct ConfigSubset *sub, struct ComposeAttachData *adata)
90 {
91  if (!adata || !adata->actx)
92  return 0;
93 
94  size_t s = 0;
95  struct Content *info = NULL;
96  struct Body *b = NULL;
97  struct AttachCtx *actx = adata->actx;
98  struct AttachPtr **idx = actx->idx;
99 
100  for (unsigned short i = 0; i < actx->idxlen; i++)
101  {
102  b = idx[i]->body;
103 
104  if (!b->content)
105  b->content = mutt_get_content_info(b->filename, b, sub);
106 
107  info = b->content;
108  if (info)
109  {
110  switch (b->encoding)
111  {
113  s += 3 * (info->lobin + info->hibin) + info->ascii + info->crlf;
114  break;
115  case ENC_BASE64:
116  s += (4 * (info->lobin + info->hibin + info->ascii + info->crlf)) / 3;
117  break;
118  default:
119  s += info->lobin + info->hibin + info->ascii + info->crlf;
120  break;
121  }
122  }
123  }
124 
125  return s;
126 }
127 
131 int attach_recalc(struct MuttWindow *win)
132 {
133 #if 0
134  struct ComposeBarData *attach_data = win->wdata;
135  struct ComposeRedrawData *rd = attach_data->rd;
136 
137  char buf[1024] = { 0 };
138  const char *const c_compose_format =
139  cs_subset_string(rd->sub, "compose_format");
140  mutt_expando_format(buf, sizeof(buf), 0, win->state.cols, NONULL(c_compose_format),
141  compose_format_str, (intptr_t) rd->menu, MUTT_FORMAT_NO_FLAGS);
142 
143  if (!mutt_str_equal(buf, attach_data->compose_format))
144  {
145  mutt_str_replace(&attach_data->compose_format, buf);
146  win->actions |= WA_REPAINT;
147  }
148 #endif
149  win->actions |= WA_REPAINT;
150  mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
151  return 0;
152 }
153 
157 int attach_repaint(struct MuttWindow *win)
158 {
159  if (!mutt_window_is_visible(win))
160  return 0;
161 #if 0
162  struct ComposeBarData *attach_data = win->wdata;
163 
164  mutt_window_move(win, 0, 0);
167 
168  mutt_window_move(win, 0, 0);
169  mutt_draw_statusline(win->state.cols, attach_data->compose_format,
170  mutt_str_len(attach_data->compose_format));
172 #endif
173  mutt_debug(LL_DEBUG5, "repaint done\n");
174  return 0;
175 }
176 
181 {
182  if ((nc->event_type != NT_COMPOSE) || !nc->global_data)
183  return -1;
184 
185  if (nc->event_subtype != NT_COMPOSE_ATTACH)
186  return 0;
187 
188  struct MuttWindow *win_attach = nc->global_data;
189 
190  win_attach->actions |= WA_RECALC;
191  mutt_debug(LL_DEBUG5, "compose done, request WA_RECALC\n");
192 
193  return 0;
194 }
195 
200 {
201  if ((nc->event_type != NT_CONFIG) || !nc->global_data || !nc->event_data)
202  return -1;
203 
204  struct EventConfig *ev_c = nc->event_data;
205  if (!mutt_str_equal(ev_c->name, "attach_format"))
206  return 0;
207 
208  struct MuttWindow *win_attach = nc->global_data;
209  win_attach->actions |= WA_RECALC;
210  mutt_debug(LL_DEBUG5, "config, request WA_RECALC\n");
211 
212  return 0;
213 }
214 
219 {
220  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
221  return -1;
222 
223  struct MuttWindow *win_attach = nc->global_data;
224  struct EventWindow *ev_w = nc->event_data;
225  if (ev_w->win != win_attach)
226  return 0;
227 
228  if (nc->event_subtype == NT_WINDOW_STATE)
229  {
230  win_attach->actions |= WA_RECALC;
231  mutt_debug(LL_DEBUG5, "window state done, request WA_RECALC\n");
232  }
233  else if (nc->event_subtype == NT_WINDOW_DELETE)
234  {
235  notify_observer_remove(win_attach->parent->notify, attach_compose_observer, win_attach);
237  notify_observer_remove(win_attach->notify, attach_window_observer, win_attach);
238  mutt_debug(LL_DEBUG5, "window delete done\n");
239  }
240 
241  return 0;
242 }
243 
247 static int compose_attach_tag(struct Menu *menu, int sel, int act)
248 {
249  struct ComposeAttachData *adata = menu->mdata;
250  struct AttachCtx *actx = adata->actx;
251  struct Body *cur = actx->idx[actx->v2r[sel]]->body;
252  bool ot = cur->tagged;
253 
254  cur->tagged = ((act >= 0) ? act : !cur->tagged);
255  return cur->tagged - ot;
256 }
257 
261 static void compose_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
262 {
263  struct ComposeAttachData *adata = menu->mdata;
264  struct AttachCtx *actx = adata->actx;
265  struct ComposeSharedData *shared = menu->win->parent->wdata;
266  struct ConfigSubset *sub = shared->sub;
267 
268  const char *const c_attach_format = cs_subset_string(sub, "attach_format");
269  mutt_expando_format(buf, buflen, 0, menu->win->state.cols, NONULL(c_attach_format),
270  attach_format_str, (intptr_t) (actx->idx[actx->v2r[line]]),
272 }
273 
279 struct MuttWindow *attach_new(struct MuttWindow *parent, struct ComposeSharedData *shared)
280 {
281  struct MuttWindow *win_attach = menu_new_window(MENU_COMPOSE, NeoMutt->sub);
282 
283  struct ComposeAttachData *adata = attach_data_new(shared->email);
284 
285  shared->adata = adata;
286 
287  // win_attach->recalc = attach_recalc;
288  // win_attach->repaint = attach_repaint;
289 
290  // NT_COLOR is handled by the Menu Window
293  notify_observer_add(win_attach->notify, NT_WINDOW, attach_window_observer, win_attach);
294 
295  struct Menu *menu = win_attach->wdata;
296  menu->pagelen = win_attach->state.rows;
297  menu->win = win_attach;
298 
300  menu->tag = compose_attach_tag;
301  menu->mdata = adata;
303  adata->menu = menu;
304 
305  return win_attach;
306 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
Convenience wrapper for the gui headers.
struct Content * mutt_get_content_info(const char *fname, struct Body *b, struct ConfigSubset *sub)
Analyze file to determine MIME encoding to use.
Definition: sendlib.c:465
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:780
char * filename
when sending a message, this is the file to which this structure refers
Definition: body.h:46
#define NONULL(x)
Definition: string2.h:37
An email to which things will be attached.
Definition: attach.h:34
struct Content * content
Detailed info about the content of the attachment.
Definition: body.h:51
Definition: lib.h:67
#define WA_REPAINT
Redraw the contents of the Window.
Definition: mutt_window.h:111
Data passed to a notification function.
Definition: observer.h:39
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
Structs that make up an email.
An Event that happened to a Window.
Definition: mutt_window.h:215
Data to fill the Compose Attach Window.
Definition: attach_data.h:32
Convenience wrapper for the send headers.
int attach_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t.
Definition: attach.c:199
Compose Shared Data.
void mutt_curses_set_color(enum ColorId color)
Set the current colour for text.
Definition: mutt_curses.c:56
size_t idx
Definition: mailbox.c:257
void mutt_window_clrtoeol(struct MuttWindow *win)
Clear to the end of the line.
Definition: mutt_window.c:259
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: format_flags.h:35
A config-change event.
Definition: subset.h:69
Shared Compose Data.
Definition: shared_data.h:29
static const char * compose_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Create the status bar string for compose mode - Implements format_t -Expando Description %a Total nu...
Definition: cbar.c:103
Window is about to be deleted.
Definition: mutt_window.h:206
short idxlen
Number of attachmentes.
Definition: attach.h:55
Compose an email.
Definition: type.h:42
long hibin
8-bit characters
Definition: content.h:35
#define MUTT_FORMAT_STAT_FILE
Used by attach_format_str.
Definition: format_flags.h:34
int attach_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition: attach.c:157
Flags to control mutt_expando_format()
Container for Accounts, Notifications.
Definition: neomutt.h:36
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
The body of an email.
Definition: body.h:34
Convenience wrapper for the config headers.
int mutt_window_move(struct MuttWindow *win, int col, int row)
Move the cursor in a Window.
Definition: mutt_window.c:310
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:105
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
Some miscellaneous functions.
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Definition: mutt_window.h:132
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
const char * attach_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Format a string for the attachment menu - Implements format_t -Expando Description %C Character set ...
Definition: recvattach.c:270
static void compose_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Format a menu item for the attachment list - Implements Menu::make_entry() -.
Definition: attach.c:261
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:76
Convenience wrapper for the core headers.
static int compose_attach_tag(struct Menu *menu, int sel, int act)
Tag an attachment - Implements Menu::tag() -.
Definition: attach.c:247
bool tagged
This attachment is tagged.
Definition: body.h:70
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition: body.h:66
unsigned long cum_attachs_size(struct ConfigSubset *sub, struct ComposeAttachData *adata)
Cumulative Attachments Size.
Definition: attach.c:89
Compose Attach Data.
Base-64 encoded text.
Definition: mime.h:52
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
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
Plain text.
Definition: color.h:58
#define NT_COMPOSE_ATTACH
Attachments have changed.
Definition: private.h:36
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:207
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
Info about an attachment.
Definition: content.h:33
static int attach_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t.
Definition: attach.c:218
A set of inherited config items.
Definition: subset.h:46
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
void * mdata
Private data.
Definition: lib.h:155
bool mutt_window_is_visible(struct MuttWindow *win)
Is the Window visible?
Definition: mutt_window.c:529
struct ComposeAttachData * attach_data_new(struct Email *e)
Create new Compose Attach Data.
Definition: attach_data.c:50
Status bar (takes a pattern)
Definition: color.h:75
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:61
long ascii
Number of ascii chars.
Definition: content.h:39
void mutt_draw_statusline(struct MuttWindow *win, int cols, const char *buf, size_t buflen)
Draw a highlighted status bar.
Definition: dlg_index.c:875
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Compose data has changed, NotifyCompose.
Definition: notify_type.h:41
Compose Private Data.
Data to fill the Compose Bar Window.
Definition: cbar_data.h:33
int(* tag)(struct Menu *menu, int sel, int act)
Definition: lib.h:130
int pagelen
Number of entries per screen.
Definition: lib.h:74
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
struct AttachCtx * actx
Definition: attach_data.h:34
void * event_data
Data from notify_send()
Definition: observer.h:44
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
struct MuttWindow * attach_new(struct MuttWindow *parent, struct ComposeSharedData *shared)
Create the Attachments Menu.
Definition: attach.c:279
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:170
long lobin
Unprintable 7-bit chars (eg., control chars)
Definition: content.h:36
Routines for managing attachments.
short * v2r
Mapping from virtual to real attachment.
Definition: attach.h:58
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:446
struct Menu * menu
Definition: attach_data.h:35
struct MuttWindow * parent
Parent Window.
Definition: mutt_window.h:135
Quoted-printable text.
Definition: mime.h:51
struct ConfigSubset * sub
Inherited config items.
Definition: lib.h:78
long crlf
\r and \n characters
Definition: content.h:38
static int attach_compose_observer(struct NotifyCallback *nc)
Notification that the Compose data has changed - Implements observer_t.
Definition: attach.c:180
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:228
int attach_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition: attach.c:131
void attach_data_free(struct Menu *menu, void **ptr)
Free the Compose Attach Data - Implements Menu::mdata_free() -.
Definition: attach_data.c:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
char * compose_format
Cached status string.
Definition: cbar_data.h:35
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
void * wdata
Private data.
Definition: mutt_window.h:145
A set of attachments.
Definition: attach.h:49
const char * name
Name of config item that changed.
Definition: subset.h:72
struct AttachPtr ** idx
Array of attachments.
Definition: attach.h:54
struct Email * email
Email being composed.
Definition: shared_data.h:33
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:31
struct ComposeAttachData * adata
Attachments.
Definition: shared_data.h:34
struct Body * body
Attachment.
Definition: attach.h:36
#define WA_RECALC
Recalculate the contents of the Window.
Definition: mutt_window.h:110