bincimap

Log | Files | Refs | LICENSE

pendingupdates.cc (8414B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    pendingupdates.cc
      5  *  
      6  *  Description:
      7  *    <--->
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #include <iostream>
     35 #include <string>
     36 #include <vector>
     37 
     38 #ifdef HAVE_CONFIG_H
     39 #include <config.h>
     40 #endif
     41 
     42 #include "session.h"
     43 #include "pendingupdates.h"
     44 #include "message.h"
     45 #include "mailbox.h"
     46 #include "io.h"
     47 
     48 using namespace ::std;
     49 using namespace Binc;
     50 
     51 //------------------------------------------------------------------------
     52 PendingUpdates::PendingUpdates(void) : expunges(), flagupdates()
     53 {
     54   recent = 0;
     55   exists = 0;
     56 
     57   newrecent = false;
     58   newexists = false;
     59 }
     60 
     61 //------------------------------------------------------------------------
     62 PendingUpdates::~PendingUpdates(void)
     63 {
     64 }
     65 
     66 //------------------------------------------------------------------------
     67 void PendingUpdates::addExpunged(unsigned int uid)
     68 {
     69   expunges.push_back(uid);
     70 }
     71 
     72 //------------------------------------------------------------------------
     73 void PendingUpdates::addFlagUpdates(unsigned int sqnr, unsigned int uid, unsigned int flags)
     74 {
     75   flagupdates[sqnr] = flags;
     76   sqnrtouid[sqnr] = uid;
     77 }
     78 
     79 //------------------------------------------------------------------------
     80 void PendingUpdates::setExists(unsigned int n)
     81 {
     82   exists = n;
     83   newexists = true;
     84 }
     85 
     86 //------------------------------------------------------------------------
     87 void PendingUpdates::setRecent(unsigned int n)
     88 {
     89   recent = n;
     90   newrecent = true;
     91 }
     92 
     93 //------------------------------------------------------------------------
     94 unsigned int PendingUpdates::getExists(void) const
     95 {
     96   return exists;
     97 }
     98 
     99 //------------------------------------------------------------------------
    100 unsigned int PendingUpdates::getRecent(void) const
    101 {
    102   return recent;
    103 }
    104 
    105 //------------------------------------------------------------------------
    106 bool PendingUpdates::newExists(void) const
    107 {
    108   return newexists;
    109 }
    110 
    111 //------------------------------------------------------------------------
    112 bool PendingUpdates::newRecent(void) const
    113 {
    114   return newrecent;
    115 }
    116 
    117 //------------------------------------------------------------------------
    118 PendingUpdates::expunged_const_iterator::expunged_const_iterator(void)
    119 {
    120 }
    121 
    122 //------------------------------------------------------------------------
    123 PendingUpdates::expunged_const_iterator::expunged_const_iterator(vector<unsigned int>::iterator i) : internal(i)
    124 {
    125 }
    126 
    127 //------------------------------------------------------------------------
    128 unsigned int PendingUpdates::expunged_const_iterator::operator * (void) const
    129 {
    130   return *internal;
    131 }
    132 
    133 //------------------------------------------------------------------------
    134 void PendingUpdates::expunged_const_iterator::operator ++ (void)
    135 {
    136   ++internal;
    137 }
    138 
    139 //------------------------------------------------------------------------
    140 bool PendingUpdates::expunged_const_iterator::operator == (expunged_const_iterator i) const
    141 {
    142   return internal == i.internal;
    143 }
    144 
    145 //------------------------------------------------------------------------
    146 bool PendingUpdates::expunged_const_iterator::operator != (expunged_const_iterator i) const
    147 {
    148   return internal != i.internal;
    149 }
    150 
    151 //------------------------------------------------------------------------
    152 PendingUpdates::expunged_const_iterator PendingUpdates::beginExpunged(void)
    153 {
    154   return expunged_const_iterator(expunges.begin());
    155 }
    156 
    157 //------------------------------------------------------------------------
    158 PendingUpdates::expunged_const_iterator PendingUpdates::endExpunged(void)
    159 {
    160   return expunged_const_iterator(expunges.end());
    161 }
    162 
    163 //------------------------------------------------------------------------
    164 PendingUpdates::flagupdates_const_iterator::flagupdates_const_iterator(void)
    165 {
    166 }
    167 
    168 //------------------------------------------------------------------------
    169 PendingUpdates::flagupdates_const_iterator::flagupdates_const_iterator(map<unsigned int, unsigned int>::iterator i, map<unsigned int, unsigned int> *sqnrmap) : internal(i)
    170 {
    171   sqnrtouid = sqnrmap;
    172 }
    173 
    174 //------------------------------------------------------------------------
    175 void PendingUpdates::flagupdates_const_iterator::operator ++ (void)
    176 {
    177   ++internal;
    178 }
    179 
    180 //------------------------------------------------------------------------
    181 bool PendingUpdates::flagupdates_const_iterator::operator != (flagupdates_const_iterator i) const
    182 {
    183   return internal != i.internal;
    184 }
    185 
    186 //------------------------------------------------------------------------
    187 PendingUpdates::flagupdates_const_iterator PendingUpdates::beginFlagUpdates(void)
    188 {
    189   return flagupdates_const_iterator(flagupdates.begin(), &sqnrtouid);
    190 }
    191 
    192 //------------------------------------------------------------------------
    193 PendingUpdates::flagupdates_const_iterator PendingUpdates::endFlagUpdates(void)
    194 {
    195   return flagupdates_const_iterator(flagupdates.end(), &sqnrtouid);
    196 }
    197 
    198 //------------------------------------------------------------------------
    199 unsigned int PendingUpdates::flagupdates_const_iterator::first(void) const
    200 {
    201   return internal->first;
    202 }
    203 
    204 //------------------------------------------------------------------------
    205 unsigned int PendingUpdates::flagupdates_const_iterator::second(void) const
    206 {
    207   return internal->second;
    208 }
    209 
    210 //------------------------------------------------------------------------
    211 unsigned int PendingUpdates::flagupdates_const_iterator::getUID(void) const
    212 {
    213   return (*sqnrtouid)[internal->first];
    214 }
    215 
    216 //--------------------------------------------------------------------
    217 bool Binc::pendingUpdates(Mailbox *mailbox, int type, bool rescan, bool showAll,
    218 			  bool forceScan, bool uidfetchflags)
    219 {
    220   Session &session = Session::getInstance();
    221   IO &com = IOFactory::getInstance().get(1);
    222 
    223   if (mailbox == 0)
    224     return true;
    225 
    226   PendingUpdates p;
    227   if (!mailbox->getUpdates(rescan, type, p, forceScan)) {
    228     session.setLastError(mailbox->getLastError());
    229     return false;
    230   }
    231 
    232   if (type & PendingUpdates::EXPUNGE) {
    233     PendingUpdates::expunged_const_iterator i = p.beginExpunged();
    234     PendingUpdates::expunged_const_iterator e = p.endExpunged();
    235 
    236     while (i != e) {
    237       com << "* " << *i << " EXPUNGE" << endl;
    238       ++i;
    239     }
    240   }
    241 
    242   if (((type & PendingUpdates::EXISTS) && p.newExists()) || showAll)
    243     com << "* " << p.getExists() << " EXISTS" << endl;
    244 
    245   if (((type & PendingUpdates::RECENT) && p.newRecent() || showAll))
    246     com << "* " << p.getRecent() << " RECENT" << endl;
    247 
    248   if (type & PendingUpdates::FLAGS) {
    249     PendingUpdates::flagupdates_const_iterator i = p.beginFlagUpdates();
    250     PendingUpdates::flagupdates_const_iterator e = p.endFlagUpdates();
    251 
    252     while (i != e) {
    253       int flags = i.second();
    254 
    255       vector<string> flagv;
    256       if (flags & Message::F_SEEN) flagv.push_back("\\Seen");
    257       if (flags & Message::F_ANSWERED) flagv.push_back("\\Answered");
    258       if (flags & Message::F_DELETED) flagv.push_back("\\Deleted");
    259       if (flags & Message::F_DRAFT) flagv.push_back("\\Draft");
    260       if (flags & Message::F_RECENT) flagv.push_back("\\Recent");
    261       if (flags & Message::F_FLAGGED) flagv.push_back("\\Flagged");
    262 
    263       com << "* " << i.first() << " FETCH (FLAGS (";
    264       for (vector<string>::const_iterator k
    265 	     = flagv.begin(); k != flagv.end(); ++k) {
    266 	if (k != flagv.begin()) com << " ";
    267 	com << *k;
    268       }
    269 	
    270       com << ")";
    271       if (uidfetchflags)
    272 	com << " UID " << i.getUID();
    273 
    274       com << ")" << endl;
    275 
    276       ++i;
    277     }
    278   }
    279 
    280   return true;
    281 }