Bumped copyright dates for 2013
[barry.git] / desktop / src / barrydesktop.cc
blob4226ba5a86111239093e5019f6abab7abd96d749
1 ///
2 /// \file barrydesktop.cc
3 /// Program entry point for the desktop GUI
4 ///
6 /*
7 Copyright (C) 2009-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 "barrydesktop.h"
23 #include "util.h"
24 #include "osbase.h"
25 #include "BaseFrame.h"
26 #include <memory>
27 #include <wx/splash.h>
28 #include <wx/mstream.h>
29 #include "wxi18n.h"
31 using namespace std;
33 UsbScanSplash::UsbScanSplash()
35 wxImage scanpng(GetImageFilename(_T("scanning.png")));
36 wxBitmap scanning(scanpng);
38 // draw i18n text on the splash screen
40 wxMemoryDC dc;
41 dc.SelectObject(scanning);
43 int pointsize = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) .GetPointSize();
44 wxFont font(pointsize + 2, wxFONTFAMILY_SWISS,
45 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
47 DrawButtonLabelDC(dc, scanning, _W("Scanning USB..."),
48 font, *wxBLACK, 70, -1, -1, -1);
51 std::auto_ptr<wxSplashScreen> splash( new wxSplashScreen(
52 scanning, wxSPLASH_CENTRE_ON_SCREEN, 0,
53 NULL, -1, wxDefaultPosition, wxDefaultSize,
54 wxSIMPLE_BORDER) );
55 splash->Show(true);
56 for( int i = 0; i < 4; i++ ) {
57 wxGetApp().Yield();
58 wxMilliSleep(250);
59 wxGetApp().Yield();
63 UsbScanSplash::~UsbScanSplash()
67 //////////////////////////////////////////////////////////////////////////////
68 // BarryDesktopApp
70 BarryDesktopApp::BarryDesktopApp()
71 : m_global_config("BarryDesktop")
72 , m_set( new OpenSync::APISet )
76 void BarryDesktopApp::Probe()
78 // start fresh
79 m_results.clear();
81 try {
82 // This can throw Usb::Error exceptions
83 Barry::Probe probe;
84 m_results = probe.GetResults();
86 catch( Usb::Error &e ) {
87 wxString msg = _W("A serious error occurred while probing the USB subsystem for BlackBerry(R) devices: ");
88 msg += wxString(e.what(), wxConvUTF8);
89 wxMessageBox(msg, _W("USB Error"), wxOK | wxICON_ERROR);
93 wxBitmap BarryDesktopApp::GetScreenshot(const Barry::ProbeResult &device) const
95 // FIXME - will need to eventually move the controller object
96 // into the main app, I think, and maybe the modes too, so
97 // that multiple menu commands can work simultaneously
99 Barry::Controller con(device);
100 Barry::Mode::JavaLoader javaloader(con);
102 javaloader.Open();
103 javaloader.StartStream();
105 Barry::JLScreenInfo info;
106 Barry::Data image;
107 javaloader.GetScreenshot(info, image);
109 // Convert to BMP format
110 Barry::Data bitmap(-1, GetTotalBitmapSize(info));
111 Barry::ScreenshotToBitmap(info, image, bitmap);
113 // Load as wxImage
114 wxMemoryInputStream stream(bitmap.GetData(), bitmap.GetSize());
115 wxImage bmp(stream, wxBITMAP_TYPE_BMP);
116 bmp.Rescale(180, 100, wxIMAGE_QUALITY_HIGH);
117 return wxBitmap(bmp);
120 void BarryDesktopApp::SetDeviceName(Barry::Pin pin, const std::string &name)
122 if( !pin.Valid() )
123 return;
125 // load device config
126 Barry::ConfigFile cfg(pin);
128 // re-save device config
129 cfg.SetDeviceName(name);
130 cfg.Save();
132 // update our results if this device exists in our list
133 int index = Barry::Probe::Find(m_results, pin);
134 if( index != -1 ) {
135 m_results[index].m_cfgDeviceName = name;
139 std::string BarryDesktopApp::GetDeviceName(Barry::Pin pin) const
141 string name;
142 int index = Barry::Probe::Find(m_results, pin);
143 if( index != -1 )
144 name = m_results[index].m_cfgDeviceName;
145 return name;
148 void BarryDesktopApp::ShowMissingOpenSyncMessage()
150 wxMessageBox(_W("No OpenSync libraries were found. Sync will be unavailable until you install OpenSync version 0.22 or version 0.4x on your system, along with the needed plugins."), _W("OpenSync Not Found"), wxOK | wxICON_INFORMATION);
153 bool BarryDesktopApp::OnInit()
155 // initialize i18n
156 INIT_I18N(PACKAGE);
158 // Add a PNG handler for loading buttons and backgrounds
159 wxImage::AddHandler( new wxPNGHandler );
161 // Add handlers for loading Contact Photos
162 wxImage::AddHandler( new wxJPEGHandler );
163 wxImage::AddHandler( new wxTIFFHandler );
164 wxImage::AddHandler( new wxXPMHandler );
166 std::auto_ptr<UsbScanSplash> splash( new UsbScanSplash );
168 // Initialize Barry and USB
169 Barry::Init(m_global_config.VerboseLogging());
171 // Scan bus at the beginning so we know what devices we've got
172 Probe();
174 // Search for available OpenSync libraries
175 if( m_set->OpenAvailable() == 0 ) {
176 ShowMissingOpenSyncMessage();
179 // Create the main frame window where all the action happens
180 wxImage back(GetImageFilename(_T("background.png")));
181 if( !back.IsOk() ) {
182 Yield();
183 return false;
185 BaseFrame *frame = new BaseFrame(back);
187 // Clean up the splash screen, and init the main frame
188 splash.reset();
189 SetTopWindow(frame);
190 frame->Show(true);
192 return true;
195 int BarryDesktopApp::OnExit()
197 try {
198 m_global_config.Save();
200 catch( std::exception &e ) {
201 cerr << "Exception caught while saving config: "
202 << e.what() << endl;
205 return 0;
208 // This takes care of main() and wxGetApp() for us.
209 IMPLEMENT_APP(BarryDesktopApp)