3 Session Class implementation.
5 Copyright (C) Alan Alvarez 2008.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <libmapi++/message_store.h>
22 #include <libmapi++/profile.h>
26 inline std::string session::get_default_profile_path()
28 const char* profile_path = getenv("HOME");
29 std::string retval = "";
31 retval = profile_path;
32 retval += "/.openchange/profiles.ldb";
38 session::session(const std::string& profiledb, bool debug) throw(std::runtime_error, mapi_exception)
39 : m_session(NULL), m_memory_ctx(talloc_named(NULL, 0, "libmapi++")), m_message_store(new message_store(*this))
41 mapi_exception::fill_status_map();
43 std::string profile_path;
45 // If profile is not provided, attempt to get it from default location
46 // (~/.openchange/profiles.ldb)
47 if (profiledb == "") {
48 profile_path = get_default_profile_path();
49 if (profile_path == "") {
50 talloc_free(m_memory_ctx);
51 delete m_message_store;
52 throw std::runtime_error("libmapipp::session(): Failed to get $HOME env variable");
55 profile_path = profiledb;
58 if (MAPIInitialize(profile_path.c_str()) != MAPI_E_SUCCESS) {
59 talloc_free(m_memory_ctx);
60 delete m_message_store;
61 throw mapi_exception(GetLastError(), "session::session : MAPIInitialize");
64 if (debug) global_mapi_ctx->dumpdata = true;
67 void session::login(const std::string& profile_name, const std::string& password) throw (mapi_exception)
69 m_profile_name = profile_name;
70 if (m_profile_name == "") { // if profile is not set, try to get default profile
72 m_profile_name = profile::get_default_profile();
73 } catch(mapi_exception e) {
79 if (MapiLogonEx(&m_session, m_profile_name.c_str(), (password != "") ? password.c_str() : 0 ) != MAPI_E_SUCCESS) {
81 throw mapi_exception(GetLastError(), "session::session : MapiLogonEx");
85 m_message_store->open(m_session);
86 } catch (mapi_exception e) {
91 inline void session::uninitialize() throw()
93 talloc_free(m_memory_ctx);
95 delete m_message_store;
98 } // namespace libmapipp