bincimap

Log | Files | Refs | LICENSE

maildir.h (5842B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    src/mailbox/maildir.h
      5  *  
      6  *  Description:
      7  *    Declaration of the Maildir class.
      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 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #ifndef maildir_h_included
     39 #define maildir_h_included
     40 #include <string>
     41 #include <vector>
     42 #include <map>
     43 
     44 #include "mailbox.h"
     45 #include "maildirmessage.h"
     46 
     47 namespace Binc {
     48   static const std::string CACHEFILEVERSION = "1.0.5";
     49   static const std::string UIDVALFILEVERSION = "1.0.5";
     50   
     51   //------------------------------------------------------------------------
     52   class MaildirIndexItem {
     53   public:
     54     unsigned int uid;
     55     std::string fileName;
     56   };
     57 
     58   //------------------------------------------------------------------------
     59   class MaildirIndex
     60   {
     61   private:
     62     std::map<std::string, MaildirIndexItem> idx;
     63 
     64   public:
     65     void insert(const std::string &unique, unsigned int uid,
     66 		const std::string &fileName = "");
     67     void remove(const std::string &unique);
     68     void clear(void);
     69     void clearFileNames(void);
     70     void clearUids(void);
     71     unsigned int getSize(void) const;
     72     MaildirIndexItem *find(const std::string &unique);
     73   };
     74 
     75   //------------------------------------------------------------------------
     76   class Maildir : public Mailbox {
     77   public:
     78     typedef std::map<unsigned int, MaildirMessage> MessageMap;
     79 
     80     class iterator : public BaseIterator {
     81     public:
     82       iterator(void);
     83       iterator(Maildir *home, MessageMap::iterator i,
     84 	       const SequenceSet &bset,
     85 	       unsigned int mod = INCLUDE_EXPUNGED | SQNR_MODE);
     86       iterator(const iterator &copy);
     87       ~iterator(void);
     88 
     89       Message &operator *(void);
     90       void operator ++(void);
     91       bool operator ==(const BaseIterator &) const;
     92       bool operator !=(const BaseIterator &) const;
     93 
     94       iterator &operator =(const iterator &copy);
     95 
     96       void erase(void);
     97 
     98       friend class Maildir;
     99 
    100     protected:
    101       void reposition(void);
    102       MaildirMessage &curMessage(void);
    103 
    104     private:
    105       Maildir *mailbox;
    106       SequenceSet bset;
    107       int mod;
    108 
    109       MessageMap::iterator i;
    110       unsigned int uidmax;
    111       unsigned int sqnrmax;
    112 
    113       iterator(iterator &external);
    114     };
    115  
    116     const std::string getTypeName(void) const;
    117 
    118     Mailbox::iterator begin(const SequenceSet &bset, unsigned int mod = INCLUDE_EXPUNGED | SQNR_MODE) const;
    119     Mailbox::iterator end(void) const;
    120 
    121     unsigned int getMaxUid(void) const;
    122     unsigned int getMaxSqnr(void) const;
    123     unsigned int getUidValidity(void) const;
    124     unsigned int getUidNext(void) const;
    125 
    126     bool getUpdates(bool doscan, unsigned int type,
    127 		    PendingUpdates &updates, bool forceScan);
    128 
    129     const std::string &getPath(void) const;
    130     void setPath(const std::string &path_in);
    131 
    132     void bumpUidValidity(const std::string &) const;
    133 
    134     unsigned int getStatusID(const std::string &) const;
    135     bool getStatus(const std::string &, Status &) const;
    136     void updateFlags(void);
    137 
    138     bool isMailbox(const std::string &) const;
    139     bool isMarked(const std::string &) const;
    140     bool selectMailbox(const std::string &name, const std::string &s_in);
    141     void closeMailbox(void);
    142     void expungeMailbox(void);
    143     bool createMailbox(const std::string &s, mode_t mode, uid_t owner = 0, gid_t group = 0, bool root = false);
    144     bool deleteMailbox(const std::string &s);
    145 
    146     Message *createMessage(const std::string &mbox, time_t idate = 0);
    147     bool commitNewMessages(const std::string &mbox);
    148     bool rollBackNewMessages(void);
    149 
    150     bool fastCopy(Message &source, Mailbox &desttype, const std::string &destname);
    151 
    152     //--
    153     Maildir(void);
    154     ~Maildir(void);
    155 
    156     friend class Maildir::iterator;
    157     friend class MaildirMessage;
    158 
    159   protected:
    160     enum ReadCacheResult {
    161       Ok,
    162       NoCache,
    163       Error
    164     };
    165 
    166     ReadCacheResult readCache(void);
    167     bool writeCache(void);
    168     bool scanFileNames(void) const;
    169 
    170     enum ScanResult {
    171       Success = 0,
    172       TemporaryError = 1,
    173       PermanentError = 2
    174     };
    175 
    176     ScanResult scan(bool forceScan = false);
    177 
    178     MaildirMessage *get(const std::string &id);
    179     void add(MaildirMessage &m);
    180 
    181   private:
    182     std::vector<MaildirMessage> newMessages;
    183 
    184     unsigned int uidvalidity;
    185     unsigned int uidnext;
    186     bool selected;
    187     std::string path;
    188 
    189     mutable iterator beginIterator;
    190     mutable iterator endIterator;
    191 
    192     mutable bool firstscan;
    193     mutable bool cacheRead;
    194     mutable MaildirIndex index;
    195     mutable MessageMap messages;
    196 
    197     mutable unsigned int oldrecent;
    198     mutable unsigned int oldexists;
    199 
    200     mutable time_t old_cur_st_mtime;
    201     mutable time_t old_cur_st_ctime;
    202     mutable time_t old_new_st_mtime;
    203     mutable time_t old_new_st_ctime;
    204 
    205     mutable bool uidnextchanged;
    206     mutable bool mailboxchanged;
    207   };
    208 }
    209 
    210 #endif