desktop: EvoCfgDlg: add default path option, and show current cfg better
[barry/progweb.git] / desktop / src / EvoCfgDlg.cc
blob4c6a850d2c40fa0980f4ebecba6667c11c4922f3
1 ///
2 /// \file EvoCfgDlg.cc
3 /// The configuration dialog used to configure Evolution sources
4 ///
6 /*
7 Copyright (C) 2011-2012, 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 #include "EvoCfgDlg.h"
23 #include "osconfig.h"
24 #include "windowids.h"
26 using namespace std;
28 EvoCfgDlg::EvoCfgDlg(wxWindow *parent,
29 const OpenSync::Config::Evolution &ec,
30 const EvoSources &es)
31 : wxDialog(parent, Dialog_EvoCfg, _T("Evolution Plugin Configuration"))
32 , m_address_path(ec.GetAddressPath())
33 , m_calendar_path(ec.GetCalendarPath())
34 , m_tasks_path(ec.GetTasksPath())
35 , m_memos_path(ec.GetMemosPath())
36 , m_empty_config(ec.GetAddressPath().empty() &&
37 ec.GetCalendarPath().empty() &&
38 ec.GetTasksPath().empty() &&
39 ec.GetMemosPath().empty())
40 , m_sources(es)
42 CreateLayout();
45 void EvoCfgDlg::CreateLayout()
47 m_topsizer = new wxBoxSizer(wxVERTICAL);
49 m_topsizer->Add(
50 new wxStaticText(this, wxID_ANY, _T("Address Book:")),
51 0, wxALIGN_LEFT | wxTOP | wxLEFT | wxRIGHT, 10);
52 AddCombo(&m_address_combo, wxID_ANY,
53 m_address_path, m_sources.GetAddressBook());
54 m_topsizer->Add(m_address_combo, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
56 m_topsizer->Add(
57 new wxStaticText(this, wxID_ANY, _T("Calendar:")),
58 0, wxALIGN_LEFT | wxTOP | wxLEFT | wxRIGHT, 10);
59 AddCombo(&m_calendar_combo, wxID_ANY,
60 m_calendar_path, m_sources.GetEvents());
61 m_topsizer->Add(m_calendar_combo, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
63 m_topsizer->Add(
64 new wxStaticText(this, wxID_ANY, _T("Tasks:")),
65 0, wxALIGN_LEFT | wxTOP | wxLEFT | wxRIGHT, 10);
66 AddCombo(&m_tasks_combo, wxID_ANY,
67 m_tasks_path, m_sources.GetTasks());
68 m_topsizer->Add(m_tasks_combo, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
70 m_topsizer->Add(
71 new wxStaticText(this, wxID_ANY, _T("Memos:")),
72 0, wxALIGN_LEFT | wxTOP | wxLEFT | wxRIGHT, 10);
73 AddCombo(&m_memos_combo, wxID_ANY,
74 m_memos_path, m_sources.GetMemos());
75 m_topsizer->Add(m_memos_combo, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
77 wxSizer *button = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
78 m_topsizer->Add(button, 0, wxALL | wxEXPAND | wxALIGN_RIGHT, 10);
80 SetSizer(m_topsizer);
81 m_topsizer->SetSizeHints(this);
82 m_topsizer->Layout();
85 void EvoCfgDlg::AddCombo(wxComboBox **combo, int id,
86 const std::string &current_path,
87 const EvoSources::List &list)
90 // Note: the evolution opensync plugins allow "default" as a
91 // configuration string, to chose to connect to the default
92 // Evolution address book or calendar. This is not detected
93 // by the EvoSources detection, since this is a special property
94 // of the opensync plugin.
96 // So we handle the "default" strings specially below.
100 // is the current path in the list?
101 bool in_list = false;
102 for( EvoSources::List::const_iterator i = list.begin(); current_path.size() && i != list.end(); ++i ) {
103 if( i->m_SourcePath == current_path ) {
104 in_list = true;
105 break;
110 // create an array of choices
112 wxArrayString choices;
114 // add current_path as first in list if it is not already there
115 if( current_path.size() && !in_list && current_path != "default" ) {
116 choices.Add(wxString(current_path.c_str(), wxConvUTF8));
119 // add the evolution special "default" option before the sources
120 // list, since it's probably what the user wants
121 choices.Add(_T("default"));
123 // add the sources list
124 for( EvoSources::List::const_iterator i = list.begin(); i!=list.end(); ++i ) {
125 if( i->m_SourcePath.size() ) {
126 choices.Add(wxString(i->m_SourcePath.c_str(), wxConvUTF8));
130 // default choice... if we have an empty config, this is the first
131 // time the user is running this dialog, so use the first option
132 // in the combo box as the default.
134 // if not an empty config, then show what the user currently
135 // has in the config, even if it is empty
136 wxString default_value;
137 if( m_empty_config && choices.GetCount() ) {
138 // first run, use first item
139 default_value = choices[0];
141 else {
142 // show user's current config
143 default_value = wxString(current_path.c_str(), wxConvUTF8);
146 // create the combo box
147 *combo = new wxComboBox(this, id,
148 default_value,
149 wxDefaultPosition, wxDefaultSize, // wxSize(250, -1), //wxDefaultSize,
150 choices,
151 wxCB_DROPDOWN);
154 void EvoCfgDlg::SetPaths(OpenSync::Config::Evolution &ec) const
156 ec.SetAddressPath(m_address_path);
157 ec.SetCalendarPath(m_calendar_path);
158 ec.SetTasksPath(m_tasks_path);
159 ec.SetMemosPath(m_memos_path);
162 wxString EvoCfgDlg::CheckPath(const wxString &name,
163 const std::string &path) const
165 if( path.size() > 7 && path.substr(0, 7) == "file://" ) {
166 std::string p = path.substr(7);
167 if( !EvoSources::PathExists(p) ) {
168 wxString msg = name + _T(" does not exist.");
169 return msg;
173 return _T("");
176 wxString EvoCfgDlg::ValidatePaths() const
178 if( m_address_path.empty() && m_calendar_path.empty() &&
179 m_tasks_path.empty() && m_memos_path.empty() )
180 return _T("No paths set! If there are no default options available in the drop down lists, please double check that you've initialized your Evolution account first.");
181 return _T("");
184 bool EvoCfgDlg::TransferDataFromWindow()
186 m_address_path = string(m_address_combo->GetValue().utf8_str());
187 m_calendar_path = string(m_calendar_combo->GetValue().utf8_str());
188 m_tasks_path = string(m_tasks_combo->GetValue().utf8_str());
189 m_memos_path = string(m_memos_combo->GetValue().utf8_str());
191 wxString msg = ValidatePaths();
192 if( msg.size() ) {
193 wxMessageBox(msg, _T("Minimum Config Required"),
194 wxOK | wxICON_ERROR, this);
195 return false;
198 return true;
201 int EvoCfgDlg::ShowModal()
203 return wxDialog::ShowModal();