Stupid winsock needs special way to close sockets.
[dftpd.git] / SymbianUI.cpp
blobe68ee06f9783587ae4800c2d44ee1fab23edb907
1 #include <eikstart.h>
2 #include <aknapp.h>
3 #include <akndoc.h>
4 #include <aknappui.h>
5 #include <aknnavide.h>
6 #include <eikedwin.h>
7 #include <aknmessagequerydialog.h>
8 #include <string>
9 #include "Log.hpp"
10 #include "LogNull.hpp"
11 #include "Server.hpp"
12 #include "ServerPtr.hpp"
13 #include "SymbianNetwork.hpp"
14 #include "AuthNone.hpp"
15 #include "AuthToken.hpp"
16 #include "Exceptions.hpp"
17 #include "resource/dftpd.hrh"
18 #include <dftpd.rsg>
20 Log* g_log = NULL;
22 // FtpAppView
24 class FtpAppView : public CCoeControl
26 public:
27 FtpAppView( const TRect& aRect );
28 virtual ~FtpAppView() {}
30 void Draw( const TRect& aRect ) const { m_view->Draw( aRect ); };
31 virtual void SizeChanged() { m_view->SetRect( Rect() ); }
33 void Log( const std::string& text );
35 CEikEdwin* m_view;
38 FtpAppView::FtpAppView( const TRect& aRect )
40 CreateWindowL();
42 m_view = new CEikEdwin;
43 m_view->SetContainerWindowL( *this );
44 m_view->ConstructL( EEikEdwinDisplayOnly | EEikEdwinNoAutoSelection );
46 SetRect( aRect );
47 ActivateL();
50 void FtpAppView::Log( const std::string& _text )
52 std::string text = _text + '\f';
54 TPtrC8 ptr( reinterpret_cast<const TUint8*>( text.c_str() ) );
55 TBuf<512> log;
56 log.FillZ();
57 log.Copy( ptr );
59 m_view->Text()->InsertL( m_view->Text()->DocumentLength(), log );
60 m_view->HandleTextChangedL();
61 m_view->SetCursorPosL( m_view->Text()->DocumentLength(), EFalse );
62 m_view->NotifyNewFormatL();
65 // FtpAppUi
67 class FtpAppUi : public CAknAppUi, public CActive, public Log
69 public:
70 FtpAppUi() : CActive( EPriorityStandard ), iAppView( NULL ), m_starter( NULL ), m_authenticationEnabled( true ) { g_log = this; }
71 virtual ~FtpAppUi();
73 void ConstructL();
75 void HandleCommandL( TInt aCommand );
76 void HandleResourceChangeL( TInt aType );
78 static TBool StartL( TAny* aThis );
79 void StartL();
81 void RunL();
82 void DoCancel();
84 void Print( const std::string& text );
86 void ServerCrashed();
88 FtpAppView* iAppView;
89 ServerPtr m_server;
90 CIdle* m_starter;
91 RTimer m_timer;
92 AuthPtr m_auth;
94 bool m_authenticationEnabled;
97 FtpAppUi::~FtpAppUi()
99 Cancel();
101 g_log = new LogNull;
103 if( m_starter )
105 m_starter->Cancel();
107 delete m_starter;
108 delete iAppView;
111 void FtpAppUi::ConstructL()
113 BaseConstructL( EAknEnableSkin );
114 iAppView = new FtpAppView( ClientRect() );
116 CActiveScheduler::Add( this );
118 m_starter = CIdle::NewL( CActive::EPriorityLow );
119 m_starter->Start( TCallBack( StartL, this ) );
122 void FtpAppUi::HandleCommandL( TInt aCommand )
124 switch( aCommand )
126 case EEikCmdExit:
127 case EAknSoftkeyExit:
128 Exit();
129 break;
131 case EGenerateToken:
132 if( m_server )
134 if( !m_authenticationEnabled )
136 g_log->Print( "Enabling authentication" );
137 m_auth.reset( new AuthToken );
138 m_server->SetAuth( m_auth );
139 m_authenticationEnabled = true;
141 ((AuthToken*)m_auth.get())->GenerateToken();
143 break;
145 case EDisableAuthentication:
146 if( m_authenticationEnabled && m_server )
148 g_log->Print( "Authentication disabled" );
149 m_auth.reset( new AuthNone );
150 m_server->SetAuth( m_auth );
151 m_authenticationEnabled = false;
153 break;
155 case EAbout:
157 CAknMessageQueryDialog* dlg = new CAknMessageQueryDialog;
158 dlg->ExecuteLD( R_DFTPD_ABOUT );
160 break;
162 default:
163 break;
167 void FtpAppUi::HandleResourceChangeL( TInt aType )
169 CAknAppUi::HandleResourceChangeL( aType );
170 if( aType == KEikDynamicLayoutVariantSwitch && iAppView != NULL )
172 iAppView->SetRect( ClientRect() );
176 TBool FtpAppUi::StartL( TAny* aThis )
178 static_cast<FtpAppUi*>( aThis )->StartL();
179 return EFalse;
182 void FtpAppUi::StartL()
184 std::string ip;
188 m_auth.reset( new AuthToken );
189 ip = EstablishConnection();
190 m_server = Server::Create( m_auth, ip );
192 catch( ServerCrash& e )
194 ServerCrashed();
195 return;
198 CEikStatusPane* sp = iEikonEnv->AppUiFactory()->StatusPane();
199 CAknNavigationControlContainer* iNaviPane = (CAknNavigationControlContainer*)sp->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) );
200 iNaviPane->Pop();
201 TPtrC8 ptr( reinterpret_cast<const TUint8*>( ip.c_str() ) );
202 TBuf<16> naviLabel;
203 naviLabel.FillZ();
204 naviLabel.Copy( ptr );
205 iNaviPane->PushL( *iNaviPane->CreateNavigationLabelL( naviLabel ) );
207 m_timer.CreateLocal();
208 m_timer.After( iStatus, 10 );
209 SetActive();
212 void FtpAppUi::RunL()
216 m_server->Tick();
218 catch( ServerCrash& e )
220 ServerCrashed();
221 return;
224 m_timer.After( iStatus, 10 );
225 SetActive();
228 void FtpAppUi::DoCancel()
230 m_timer.Close();
233 void FtpAppUi::Print( const std::string& text )
235 iAppView->Log( text );
238 void FtpAppUi::ServerCrashed()
240 CEikStatusPane* sp = iEikonEnv->AppUiFactory()->StatusPane();
241 CAknNavigationControlContainer* iNaviPane = (CAknNavigationControlContainer*)sp->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) );
242 iNaviPane->Pop();
243 _LIT( naviLabel, "Server crashed" );
244 iNaviPane->PushL( *iNaviPane->CreateNavigationLabelL( naviLabel ) );
246 m_server.reset();
249 // FtpDocument
251 class FtpDocument : public CAknDocument
253 public:
254 FtpDocument( CEikApplication& aApp ) : CAknDocument( aApp ) {}
255 CEikAppUi* CreateAppUiL() { return static_cast<CEikAppUi*>( new FtpAppUi ); }
258 // FtpApplication
260 const TUid FtpUid = { 0xA0102039 };
262 class FtpApplication : public CAknApplication
264 public:
265 TUid AppDllUid() const { return FtpUid; }
266 CApaDocument* CreateDocumentL() { return static_cast<CApaDocument*>( new FtpDocument( *this ) ); }
269 // Startup
271 LOCAL_C CApaApplication* NewApplication()
273 return new FtpApplication;
276 GLDEF_C TInt E32Main()
278 return EikStart::RunApplication( NewApplication );