// common/DmDatabase.hh
// This file is part of Decimail; see http://decimail.org
// (C) 2004 Philip Endecott
 
// This is version $Name$
//   (if there is no version (e.g. V0-1) mentioned in the previous line,
//    this is probably a snapshot from between "official" releases.)
 
// 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
// 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
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
#ifndef DmDatabase_hh
#define DmDatabase_hh

#include "Database.hh"
#include "Message.hh"
#include "Exception.hh"

#include <string>
#include <list>
using namespace std;


class DmDatabase: public Database {

public:

  class MailboxDoesNotExist: public Exception {
  private:
    string mailbox_name;
  public:
    MailboxDoesNotExist(string m): mailbox_name(m) {};
    void report (ostream& s);
  };

  class MailboxExists: public Exception {
  private:
    string mailbox_name;
  public:
    MailboxExists(string m): mailbox_name(m) {};
    void report (ostream& s);
  };


  DmDatabase(void);

  string get_mailbox_query(string username, string mailbox_name);

  auto_ptr<list<Message*> > get_messages(const list<int>& msg_ids);

  class FlagExists: public Exception {
  private:
    string flag;
  public:
    FlagExists(string f): flag(f) {}
    void report(ostream& s);
  };

  class FlagDoesNotExist: public Exception {
  private:
    string flag;
  public:
    FlagDoesNotExist(string f): flag(f) {}
    void report(ostream& s);
  };

  void set_imap_mailbox_flag(string username, string mailbox_name, string flag);
  void clear_imap_mailbox_flag(string username, string mailbox_name, string flag);

  void set_imap_message_flag(string username, int msg_id, string flag);
  void clear_imap_message_flag(string username, int msg_id, string flag);
  void clear_all_imap_message_flags(string username, int msg_id);
  list<string> get_imap_message_flags(string username, int msd_id);

  void insert(const Message& message, string owner);

  int get_next_uid(void);

  void listen_for_new_messages(string username);
  bool check_for_new_messages(void);

  bool check_password(string username, string passwd);

private:
  void insert_addrs(int msg_id, string what_addr,
		    const mimetic::AddressList addrs);

};



#endif
