tools: added -i charset support to btardump
[barry.git] / src / m_desktoptmpl.h
blob15a0d86554fc6f03fffc88838dcc90460f32d0a6
1 ///
2 /// \file m_desktoptmpl.h
3 /// Ease of use templates for the Desktop mode class
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_M_DESKTOPTMPL_H__
23 #define __BARRY_M_DESKTOPTMPL_H__
25 #include "dll.h"
26 #include "m_desktop.h"
27 #include "parser.h"
28 #include "builder.h"
30 namespace Barry { namespace Mode {
32 BXEXPORT void LoadDatabase(unsigned int dbId, Parser &parser);
33 BXEXPORT void SaveDatabase(unsigned int dbId, Builder &builder);
35 template <class RecordT, class StorageT>
36 void Desktop::LoadDatabaseByType(StorageT &store)
38 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
39 Barry::RecordParser<RecordT, StorageT> parser(store);
40 this->LoadDatabase(dbId, parser);
43 template <class RecordT, class StorageT>
44 void Desktop::SaveDatabaseByType(StorageT &store)
46 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
47 Barry::RecordBuilder<RecordT, StorageT> build(store);
48 SaveDatabase(dbId, build);
51 template <class StorageT>
52 void Desktop::LoadDatabaseByName(const std::string &name, StorageT &store)
54 if( name == Contact::GetDBName() )
55 LoadDatabaseByType<Contact>(store);
56 else if( name == Message::GetDBName() )
57 LoadDatabaseByType<Message>(store);
58 else if( name == Calendar::GetDBName() )
59 LoadDatabaseByType<Calendar>(store);
60 else if( name == CalendarAll::GetDBName() )
61 LoadDatabaseByType<CalendarAll>(store);
62 else
63 throw Error("Unknown database name in LoadDatabaseByName: " + name);
66 template <class StorageT>
67 void Desktop::SaveDatabaseByName(const std::string &name, StorageT &store)
69 if( name == Contact::GetDBName() )
70 SaveDatabaseByType<Contact>(store);
71 else if( name == Message::GetDBName() )
72 SaveDatabaseByType<Message>(store);
73 else if( name == Calendar::GetDBName() )
74 SaveDatabaseByType<Calendar>(store);
75 else if( name == CalendarAll::GetDBName() )
76 SaveDatabaseByType<CalendarAll>(store);
77 else
78 throw Error("Unknown database name in SaveDatabaseByName: " + name);
81 template <class RecordT>
82 void Desktop::AddRecordByType(uint32_t recordId, const RecordT &rec)
84 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
85 // FIXME - I know this is a convenience template, but it still
86 // hurts making a temporary copy just to set the record ID...
87 // create a better API for this.
88 RecordT r = rec;
89 r.SetIds(RecordT::GetDefaultRecType(), recordId);
90 Barry::RecordFetch<RecordT> fetch(r);
91 Barry::RecordBuilder<RecordT, Barry::RecordFetch<RecordT> > build(fetch);
92 this->AddRecord(dbId, build);
96 }} // namespace Barry::Mode
98 #endif