menu: added new Keywords tag to .desktop files
[barry.git] / src / m_mode_base.cc
blobf596d827603481cf84a9ed9d3bc6020f0fdaef75
1 ///
2 /// \file m_mode_base.cc
3 /// Base for mode classes
4 ///
6 /*
7 Copyright (C) 2005-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 "i18n.h"
23 #include "m_mode_base.h"
25 namespace Barry { namespace Mode {
27 //////////////////////////////////////////////////////////////////////////////
28 // Mode base class
30 Mode::Mode(Controller &con, Controller::ModeType type)
31 : m_con(con)
32 , m_modetype(type)
33 , m_ModeSocket(0)
37 Mode::~Mode()
42 // Open
44 /// Select device mode. This is required before using any other mode-based
45 /// operations, such as GetDBDB() and LoadDatabase().
46 ///
47 /// This function opens a socket to the device for communicating in Desktop
48 /// mode. If the device requires it, specify the password with a const char*
49 /// string in password. The password will not be stored in memory
50 /// inside this class, only a hash will be generated from it. After
51 /// using the hash, the hash memory will be set to 0. The application
52 /// is responsible for safely handling the raw password data.
53 ///
54 /// You can retry the password by catching Barry::BadPassword and
55 /// calling RetryPassword() with the new password.
56 ///
57 /// \exception Barry::Error
58 /// Thrown on protocol error.
59 ///
60 /// \exception std::logic_error()
61 /// Thrown if unsupported mode is requested, or if socket
62 /// already open.
63 ///
64 /// \exception Barry::BadPassword
65 /// Thrown when password is invalid or if not enough retries
66 /// left in the device.
67 ///
68 void Mode::Open(const char *password)
70 if( m_ModeSocket ) {
71 m_socket->Close();
72 m_socket.reset();
73 m_ModeSocket = 0;
76 m_ModeSocket = m_con.SelectMode(m_modetype);
77 RetryPassword(password);
81 // Open
83 /// Select device mode. This is required before using any other mode-based
84 /// operations, such as GetDBDB() and LoadDatabase().
85 ///
86 /// This function opens a socket to the device for communicating in Desktop
87 /// mode. If the device requires it, specify the password with a const char*
88 /// string in password. The password will not be stored in memory
89 /// inside this class, only a hash will be generated from it. After
90 /// using the hash, the hash memory will be set to 0. The application
91 /// is responsible for safely handling the raw password data.
92 ///
93 /// It uses the provided name as the name for the socket used in this mode.
94 /// Usually this shouldn't be needed unless using the raw channel mode.
95 ///
96 /// You can retry the password by catching Barry::BadPassword and
97 /// calling RetryPassword() with the new password.
98 ///
99 /// \exception Barry::Error
100 /// Thrown on protocol error.
102 /// \exception std::logic_error()
103 /// Thrown if unsupported mode is requested, or if socket
104 /// already open.
106 /// \exception Barry::BadPassword
107 /// Thrown when password is invalid or if not enough retries
108 /// left in the device.
110 void Mode::Open(const char *password, const char *name)
112 if( m_ModeSocket ) {
113 m_socket->Close();
114 m_socket.reset();
115 m_ModeSocket = 0;
118 m_ModeSocket = m_con.SelectMode(m_modetype, name);
119 RetryPassword(password);
123 // RetryPassword
125 /// Retry a failed password attempt from the first call to Open().
126 /// Only call this function in response to Barry::BadPassword exceptions
127 /// that are thrown from Open().
129 /// \exception Barry::Error
130 /// Thrown on protocol error.
132 /// \exception std::logic_error()
133 /// Thrown if in unsupported mode, or if socket already open.
135 /// \exception Barry::BadPassword
136 /// Thrown when password is invalid or if not enough retries
137 /// left in the device.
139 void Mode::RetryPassword(const char *password)
141 if( m_socket.get() != 0 )
142 throw std::logic_error(_("Socket alreay open in RetryPassword"));
144 SocketRoutingQueue::SocketDataHandlerPtr handler = GetHandler();
145 if( handler.get() ) {
146 m_socket = m_con.OpenSocket(handler, m_ModeSocket, password);
148 else {
149 m_socket = m_con.OpenSocket(m_ModeSocket, password);
152 // success... perform open-oriented setup
153 OnOpen();
157 void Mode::OnOpen()
161 SocketRoutingQueue::SocketDataHandlerPtr Mode::GetHandler()
163 // Default to returning a NULL handler so that the
164 // socket open without handler is used.
165 return SocketRoutingQueue::SocketDataHandlerPtr();
169 // FIXME - is this necessary? and if it is, wouldn't it be better
170 // in the m_jvmdebug mode class? I'm not convinced that applications
171 // should have to bother with socket-level details.
172 //void Mode::Close()
174 // if( m_ModeSocket ) {
175 // m_socket->Close();
176 // m_socket.reset();
177 // m_ModeSocket = 0;
178 // }
181 }} // namespace Barry::Mode