flags.c 6.1 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
16
 *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Thomas Roessler's avatar
Thomas Roessler committed
17 18 19 20 21
 */ 

#include "mutt.h"
#include "mutt_curses.h"
#include "sort.h"
Thomas Roessler's avatar
Thomas Roessler committed
22
#include "mx.h"
Thomas Roessler's avatar
Thomas Roessler committed
23

24
void _mutt_set_flag (CONTEXT *ctx, HEADER *h, int flag, int bf, int upd_ctx)
Thomas Roessler's avatar
Thomas Roessler committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
{
  int changed = h->changed;
  int deleted = ctx->deleted;
  int tagged = ctx->tagged;

  if (ctx->readonly && flag != M_TAG)
    return; /* don't modify anything if we are read-only */

  switch (flag)
  {
    case M_DELETE:
      if (bf)
      {
	if (!h->deleted)
	{
	  h->deleted = 1;
41
	  if (upd_ctx) ctx->deleted++;
42 43 44 45 46 47 48 49 50
#ifdef USE_IMAP
          /* deleted messages aren't treated as changed elsewhere so that the
           * purge-on-sync option works correctly. This isn't applicable here */
          if (ctx->magic == M_IMAP)
          {
            h->changed = 1;
	    if (upd_ctx) ctx->changed = 1;
          }
#endif
Thomas Roessler's avatar
Thomas Roessler committed
51 52 53 54 55
	}
      }
      else if (h->deleted)
      {
	h->deleted = 0;
56
	if (upd_ctx) ctx->deleted--;
57
#ifdef USE_IMAP
58
        /* see my comment above */
59
	if (ctx->magic == M_IMAP) 
Thomas Roessler's avatar
Thomas Roessler committed
60 61
	{
	  h->changed = 1;
62
	  if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
63
	}
64
#endif
Thomas Roessler's avatar
Thomas Roessler committed
65 66 67 68 69 70 71 72 73
      }
      break;

    case M_NEW:
      if (bf)
      {
	if (h->read || h->old)
	{
	  h->old = 0;
74
	  if (upd_ctx) ctx->new++;
Thomas Roessler's avatar
Thomas Roessler committed
75 76 77
	  if (h->read)
	  {
	    h->read = 0;
78
	    if (upd_ctx) ctx->unread++;
Thomas Roessler's avatar
Thomas Roessler committed
79 80
	  }
	  h->changed = 1;
81
	  if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
82 83 84 85 86
	}
      }
      else if (!h->read)
      {
	if (!h->old)
87
	  if (upd_ctx) ctx->new--;
Thomas Roessler's avatar
Thomas Roessler committed
88
	h->read = 1;
89
	if (upd_ctx) ctx->unread--;
Thomas Roessler's avatar
Thomas Roessler committed
90
	h->changed = 1;
91
	if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
92 93 94 95 96 97 98 99 100 101
      }
      break;

    case M_OLD:
      if (bf)
      {
	if (!h->old)
	{
	  h->old = 1;
	  if (!h->read)
102
	    if (upd_ctx) ctx->new--;
Thomas Roessler's avatar
Thomas Roessler committed
103
	  h->changed = 1;
104
	  if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
105 106 107 108 109 110
	}
      }
      else if (h->old)
      {
	h->old = 0;
	if (!h->read)
111
	  if (upd_ctx) ctx->new++;
Thomas Roessler's avatar
Thomas Roessler committed
112
	h->changed = 1;
113
	if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
114 115 116 117 118 119 120 121 122
      }
      break;

    case M_READ:
      if (bf)
      {
	if (!h->read)
	{
	  h->read = 1;
123
	  if (upd_ctx) ctx->unread--;
Thomas Roessler's avatar
Thomas Roessler committed
124
	  if (!h->old)
125
	    if (upd_ctx) ctx->new--;
Thomas Roessler's avatar
Thomas Roessler committed
126
	  h->changed = 1;
127
	  if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
128 129 130 131 132
	}
      }
      else if (h->read)
      {
	h->read = 0;
133
	if (upd_ctx) ctx->unread++;
Thomas Roessler's avatar
Thomas Roessler committed
134
	if (!h->old)
135
	  if (upd_ctx) ctx->new++;
Thomas Roessler's avatar
Thomas Roessler committed
136
	h->changed = 1;
137
	if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
138 139 140 141 142 143 144 145 146 147 148 149
      }
      break;

    case M_REPLIED:
      if (bf)
      {
	if (!h->replied)
	{
	  h->replied = 1;
	  if (!h->read)
	  {
	    h->read = 1;
150
	    if (upd_ctx) ctx->unread--;
Thomas Roessler's avatar
Thomas Roessler committed
151
	    if (!h->old)
152
	      if (upd_ctx) ctx->new--;
Thomas Roessler's avatar
Thomas Roessler committed
153 154
	  }
	  h->changed = 1;
155
	  if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
156 157 158 159 160 161
	}
      }
      else if (h->replied)
      {
	h->replied = 0;
	h->changed = 1;
162
	if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
163 164 165 166 167 168 169 170 171
      }
      break;

    case M_FLAG:
      if (bf)
      {
	if (!h->flagged)
	{
	  h->flagged = bf;
172
	  if (upd_ctx) ctx->flagged++;
Thomas Roessler's avatar
Thomas Roessler committed
173
	  h->changed = 1;
174
	  if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
175 176 177 178 179
	}
      }
      else if (h->flagged)
      {
	h->flagged = 0;
180
	if (upd_ctx) ctx->flagged--;
Thomas Roessler's avatar
Thomas Roessler committed
181
	h->changed = 1;
182
	if (upd_ctx) ctx->changed = 1;
Thomas Roessler's avatar
Thomas Roessler committed
183 184 185 186 187 188 189 190 191
      }
      break;

    case M_TAG:
      if (bf)
      {
	if (!h->tagged)
	{
	  h->tagged = 1;
192
	  if (upd_ctx) ctx->tagged++;
Thomas Roessler's avatar
Thomas Roessler committed
193 194 195 196 197
	}
      }
      else if (h->tagged)
      {
	h->tagged = 0;
198
	if (upd_ctx) ctx->tagged--;
Thomas Roessler's avatar
Thomas Roessler committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      }
      break;
  }

  mutt_set_header_color(ctx, h);

  /* if the message status has changed, we need to invalidate the cached
   * search results so that any future search will match the current status
   * of this message and not what it was at the time it was last searched.
   */
  if (h->searched && (changed != h->changed || deleted != ctx->deleted || tagged != ctx->tagged))
    h->searched = 0;
}

void mutt_tag_set_flag (int flag, int bf)
{
  int j;

  for (j = 0; j < Context->vcount; j++)
    if (Context->hdrs[Context->v2r[j]]->tagged)
      mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], flag, bf);
}

int mutt_thread_set_flag (HEADER *cur, int flag, int bf, int subthread)
{
  HEADER *start;
  
  if ((Sort & SORT_MASK) != SORT_THREADS)
  {
228
    mutt_error _("Threading is not enabled.");
Thomas Roessler's avatar
Thomas Roessler committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    return (-1);
  }

  if (!subthread)
    while (cur->parent)
      cur = cur->parent;
  start = cur;
  
  mutt_set_flag (Context, cur, flag, bf);
  if ((cur = cur->child) == NULL)
    return (0);
  FOREVER
  {
    mutt_set_flag (Context, cur, flag, bf);
    if (cur->child)
      cur = cur->child;
    else if (cur->next)
      cur = cur->next;
    else 
    {
      while (!cur->next)
      {
	cur = cur->parent;
	if (cur == start)
	  return (0);
      }
      cur = cur->next;
    }
  }
  /* not reached */
}

int mutt_change_flag (HEADER *h, int bf)
{
  int i, flag;
264
  event_t event;
Thomas Roessler's avatar
Thomas Roessler committed
265

266
  mvprintw (LINES - 1, 0, "%s? (D/N/O/r/*/!): ", bf ? _("Set flag") : _("Clear flag"));
Thomas Roessler's avatar
Thomas Roessler committed
267 268
  clrtoeol ();

269 270 271
  event = mutt_getch();
  i = event.ch;
  if (i == -1)
Thomas Roessler's avatar
Thomas Roessler committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  {
    CLEARLINE (LINES-1);
    return (-1);
  }

  CLEARLINE (LINES-1);

  switch (i)
  {
    case 'd':
    case 'D':
      flag = M_DELETE;
      break;

    case 'N':
    case 'n':
      flag = M_NEW;
      break;

    case 'o':
    case 'O':
      if (h)
	mutt_set_flag (Context, h, M_READ, !bf);
      else
	mutt_tag_set_flag (M_READ, !bf);
      flag = M_OLD;
      break;

    case 'r':
    case 'R':
      flag = M_REPLIED;
      break;

    case '*':
      flag = M_TAG;
      break;

    case '!':
      flag = M_FLAG;
      break;

    default:
      BEEP ();
      return (-1);
  }

  if (h)
    mutt_set_flag (Context, h, flag, bf);
  else
    mutt_tag_set_flag (flag, bf);

  return 0;
}