desktop: string conversion to support i18n
[barry.git] / desktop / src / barrydesktop.cc
blob5a0d3d4c72976427630df1858f715733d343a51f
1 ///
2 /// \file barrydesktop.cc
3 /// Program entry point for the desktop GUI
4 ///
6 /*
7 Copyright (C) 2009-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 "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);
37 std::auto_ptr<wxSplashScreen> splash( new wxSplashScreen(
38 scanning, wxSPLASH_CENTRE_ON_SCREEN, 0,
39 NULL, -1, wxDefaultPosition, wxDefaultSize,
40 wxSIMPLE_BORDER) );
41 splash->Show(true);
42 wxGetApp().Yield();
43 wxGetApp().Yield();
46 UsbScanSplash::~UsbScanSplash()
50 //////////////////////////////////////////////////////////////////////////////
51 // BarryDesktopApp
53 BarryDesktopApp::BarryDesktopApp()
54 : m_global_config("BarryDesktop")
55 , m_set( new OpenSync::APISet )
59 void BarryDesktopApp::Probe()
61 // start fresh
62 m_results.clear();
64 try {
65 // This can throw Usb::Error exceptions
66 Barry::Probe probe;
67 m_results = probe.GetResults();
69 catch( Usb::Error &e ) {
70 wxString msg = _W("A serious error occurred while probing the USB subsystem for BlackBerry(R) devices: ");
71 msg += wxString(e.what(), wxConvUTF8);
72 wxMessageBox(msg, _W("USB Error"), wxOK | wxICON_ERROR);
76 wxBitmap BarryDesktopApp::GetScreenshot(const Barry::ProbeResult &device) const
78 // FIXME - will need to eventually move the controller object
79 // into the main app, I think, and maybe the modes too, so
80 // that multiple menu commands can work simultaneously
82 Barry::Controller con(device);
83 Barry::Mode::JavaLoader javaloader(con);
85 javaloader.Open();
86 javaloader.StartStream();
88 Barry::JLScreenInfo info;
89 Barry::Data image;
90 javaloader.GetScreenshot(info, image);
92 // Convert to BMP format
93 Barry::Data bitmap(-1, GetTotalBitmapSize(info));
94 Barry::ScreenshotToBitmap(info, image, bitmap);
96 // Load as wxImage
97 wxMemoryInputStream stream(bitmap.GetData(), bitmap.GetSize());
98 wxImage bmp(stream, wxBITMAP_TYPE_BMP);
99 bmp.Rescale(180, 100, wxIMAGE_QUALITY_HIGH);
100 return wxBitmap(bmp);
103 void BarryDesktopApp::SetDeviceName(Barry::Pin pin, const std::string &name)
105 if( !pin.Valid() )
106 return;
108 // load device config
109 Barry::ConfigFile cfg(pin);
111 // re-save device config
112 cfg.SetDeviceName(name);
113 cfg.Save();
115 // update our results if this device exists in our list
116 int index = Barry::Probe::Find(m_results, pin);
117 if( index != -1 ) {
118 m_results[index].m_cfgDeviceName = name;
122 std::string BarryDesktopApp::GetDeviceName(Barry::Pin pin) const
124 string name;
125 int index = Barry::Probe::Find(m_results, pin);
126 if( index != -1 )
127 name = m_results[index].m_cfgDeviceName;
128 return name;
131 void BarryDesktopApp::ShowMissingOpenSyncMessage()
133 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);
136 bool BarryDesktopApp::OnInit()
138 // Add a PNG handler for loading buttons and backgrounds
139 wxImage::AddHandler( new wxPNGHandler );
141 // Add handlers for loading Contact Photos
142 wxImage::AddHandler( new wxJPEGHandler );
143 wxImage::AddHandler( new wxTIFFHandler );
144 wxImage::AddHandler( new wxXPMHandler );
146 std::auto_ptr<UsbScanSplash> splash( new UsbScanSplash );
148 // Initialize Barry and USB
149 Barry::Init(m_global_config.VerboseLogging());
151 // Scan bus at the beginning so we know what devices we've got
152 Probe();
154 // Search for available OpenSync libraries
155 if( m_set->OpenAvailable() == 0 ) {
156 ShowMissingOpenSyncMessage();
159 // Create the main frame window where all the action happens
160 wxImage back(GetImageFilename(_T("background.png")));
161 if( !back.IsOk() ) {
162 Yield();
163 return false;
165 BaseFrame *frame = new BaseFrame(back);
167 // Clean up the splash screen, and init the main frame
168 splash.reset();
169 SetTopWindow(frame);
170 frame->Show(true);
172 return true;
175 int BarryDesktopApp::OnExit()
177 try {
178 m_global_config.Save();
180 catch( std::exception &e ) {
181 cerr << "Exception caught while saving config: "
182 << e.what() << endl;
185 return 0;
188 // This takes care of main() and wxGetApp() for us.
189 IMPLEMENT_APP(BarryDesktopApp)