Fixed C++ errors and warnings reported by cppcheck
[barry.git] / desktop / src / CUI_Barry.cc
blobdc73eff7933e83e57755aa9975b5bb0e610989d7
1 ///
2 /// \file CUI_Barry.cc
3 /// ConfigUI derived class to configure the Barry "App"
4 ///
6 /*
7 Copyright (C) 2010-2013, 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 "CUI_Barry.h"
23 #include "barrydesktop.h"
24 #include <iostream>
25 #include <sstream>
26 #include "wxi18n.h"
28 using namespace std;
30 namespace AppConfig {
32 Barry::Barry()
33 : m_parent(0)
37 std::string Barry::AppName() const
39 return OpenSync::Config::Barry::AppName();
42 bool Barry::Configure(wxWindow *parent, plugin_ptr old_plugin)
44 return false;
47 ConfigUI::plugin_ptr Barry::GetPlugin()
49 return m_container;
52 bool Barry::RunApp(wxWindow *parent)
54 return false;
57 void Barry::PreSyncAppInit()
59 // nothing to do
62 bool Barry::ZapData(wxWindow *parent,
63 plugin_ptr plugin,
64 OpenSync::API *engine)
66 try {
68 m_parent = parent;
70 // extract OpenSync::Config::Barry from plugin
71 // this *can* throw an exception if the wrong plugin is
72 // passed in, but we want this... such an exception would
73 // represent a bug in the app, not a runtime error
74 OpenSync::Config::Barry &barry =
75 dynamic_cast<OpenSync::Config::Barry&>(*plugin);
77 // build device name
78 string device_name;
79 const ::Barry::Probe::Results &results = wxGetApp().GetResults();
80 int index = ::Barry::Probe::Find(results, barry.GetPin());
81 if( index != -1 )
82 device_name = results[index].GetDisplayName();
83 else
84 device_name = barry.GetPin().Str(); // default to PIN if not in list
86 // build intro message
87 wxString msg = wxString::Format(
88 _W("Please select the databases you wish to erase\n"
89 "on device: %s\n"
90 "\n"
91 "Note: all synced databases must be erased\n"
92 "to avoid a slow-sync."),
93 wxString(device_name.c_str(), wxConvUTF8).c_str());
95 // build list of databases (base on information from engine, if
96 // the pointer is valid)
97 wxArrayString dbs;
98 wxArrayInt selections;
99 if( !engine || (barry.GetSupportedSyncTypes(*engine) & PST_CONTACTS) ) {
100 dbs.Add( wxString(::Barry::Contact::GetDBName(), wxConvUTF8) );
101 selections.Add(dbs.GetCount() - 1);
103 if( !engine || (barry.GetSupportedSyncTypes(*engine) & PST_EVENTS) ) {
104 dbs.Add( wxString(::Barry::Calendar::GetDBName(), wxConvUTF8) );
105 selections.Add(dbs.GetCount() - 1);
107 if( !engine || (barry.GetSupportedSyncTypes(*engine) & PST_NOTES) ) {
108 dbs.Add( wxString(::Barry::Memo::GetDBName(), wxConvUTF8) );
109 selections.Add(dbs.GetCount() - 1);
111 if( !engine || (barry.GetSupportedSyncTypes(*engine) & PST_TODOS) ) {
112 dbs.Add( wxString(::Barry::Task::GetDBName(), wxConvUTF8) );
113 selections.Add(dbs.GetCount() - 1);
116 // present the list to the user
117 int count = wxGetMultipleChoices(selections, msg,
118 _W("Select Databases to Erase"), dbs, m_parent);
119 if( count <= 0 )
120 return false; // nothing to do
122 // display selections to the user for one final confirmation
123 ostringstream oss;
124 oss << _C("You have selected the following databases to be completely "
125 "erased from device: ") << device_name << "\n\n";
126 for( size_t i = 0; i < selections.GetCount(); i++ ) {
127 oss << string(dbs[selections[i]].utf8_str()) << "\n";
129 oss << "\n" << _C("Proceed with erase?");
130 wxString confirm(oss.str().c_str(), wxConvUTF8);
131 int choice = wxMessageBox(confirm, _W("Erase Confirmation"),
132 wxYES_NO | wxICON_QUESTION, m_parent);
133 if( choice != wxYES )
134 return false; // nothing to do
136 // connect to the device and delete all selected databases
137 wxBusyCursor wait;
138 ::Barry::Controller con(results[index]);
139 ::Barry::Mode::Desktop desktop(con);
140 desktop.Open();
141 const ::Barry::DatabaseDatabase &dbdb = desktop.GetDBDB();
143 for( size_t i = 0; i < selections.GetCount(); i++ ) {
145 string dbname(dbs[selections[i]].utf8_str());
147 unsigned int dbid;
148 if( !dbdb.GetDBNumber(dbname, dbid) ) {
149 barryverbose(_C("No database named '") << dbname << _C("' in device!"));
150 continue;
153 barryverbose(_C("Clearing db: ") << dbname);
154 desktop.ClearDatabase(dbid);
157 return true;
159 } catch( ::Barry::Error &e ) {
160 ostringstream oss;
161 oss << _C("Barry exception: ") << e.what() << "\n\n"
162 << _C("You may need to do a USB reset and rescan from the "
163 "main menu.");
164 wxString msg(oss.str().c_str(), wxConvUTF8);
165 wxMessageBox(msg, _W("Barry Exception"),
166 wxOK | wxICON_ERROR, m_parent);
167 return false;