"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/WaitDialog.h" (10 Oct 2018, 6193 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "WaitDialog.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.22_Source_vs_1.23_Source.

    1 /*
    2  Copyright (c) 2013-2018 IDRIX. All rights reserved.
    3 
    4  Governed by the Apache License 2.0 the full text of which is
    5  contained in the file License.txt included in VeraCrypt binary and source
    6  code distribution packages.
    7 */
    8 
    9 #ifndef TC_HEADER_Main_Forms_WaitDialog
   10 #define TC_HEADER_Main_Forms_WaitDialog
   11 
   12 #include "Forms.h"
   13 #include "Main/Main.h"
   14 #include "Main/Application.h"
   15 #include <wx/msgqueue.h>
   16 
   17 namespace VeraCrypt
   18 {
   19 
   20     DECLARE_LOCAL_EVENT_TYPE(wxEVT_COMMAND_WAITDIALOGTHREAD_COMPLETED, -1);
   21     DECLARE_LOCAL_EVENT_TYPE(wxEVT_COMMAND_WAITDIALOG_ADMIN_PASSWORD, -1);
   22     DECLARE_LOCAL_EVENT_TYPE(wxEVT_COMMAND_WAITDIALOG_PIN, -1);
   23     DECLARE_LOCAL_EVENT_TYPE(wxEVT_COMMAND_WAITDIALOG_SHOW_MSG, -1);
   24 
   25     class WaitDialog;
   26 
   27 
   28 
   29     class WaitThread : public wxThread
   30     {
   31     public:
   32         WaitThread(WaitDialog *handler, WaitThreadRoutine* pRoutine) : wxThread(wxTHREAD_DETACHED), m_pRoutine(pRoutine)
   33         {
   34             m_pHandler = handler;
   35         }
   36         ~WaitThread()
   37         {
   38         }
   39 
   40     protected:
   41         virtual ExitCode Entry();
   42         WaitDialog *m_pHandler;
   43         WaitThreadRoutine* m_pRoutine;
   44     };
   45 
   46     class WaitDialog : public WaitDialogBase, public WaitThreadUI
   47     {
   48     public:
   49         WaitDialog (wxWindow *parent, const wxString& label, WaitThreadRoutine* pRoutine)
   50             : WaitDialogBase(parent), WaitThreadUI(pRoutine), m_bThreadRunning (false), m_timer (this)
   51         {
   52             WaitStaticText->SetLabel (label);
   53             WaitProgessBar->Pulse();
   54             Layout();
   55             GetSizer()->Fit( this );
   56             Centre( wxBOTH );
   57             Connect( wxID_ANY, wxEVT_COMMAND_WAITDIALOGTHREAD_COMPLETED, wxCommandEventHandler( WaitDialog::OnThreadCompletion ) );
   58             Connect( wxID_ANY, wxEVT_COMMAND_WAITDIALOG_ADMIN_PASSWORD, wxCommandEventHandler( WaitDialog::OnAdminPasswordRequest ) );
   59             Connect( wxID_ANY, wxEVT_COMMAND_WAITDIALOG_PIN, wxCommandEventHandler( WaitDialog::OnPinRequest ) );
   60             Connect( wxID_ANY, wxEVT_COMMAND_WAITDIALOG_SHOW_MSG, wxCommandEventHandler( WaitDialog::OnShowMsg ) );
   61 
   62             Connect( wxEVT_TIMER, wxTimerEventHandler( WaitDialog::OnProgressTimer ), NULL, this );
   63             m_thread = new WaitThread(this, pRoutine);
   64         }
   65 
   66         ~WaitDialog()
   67         {
   68             Disconnect( wxEVT_TIMER, wxTimerEventHandler( WaitDialog::OnProgressTimer ));
   69             Disconnect( wxID_ANY, wxEVT_COMMAND_WAITDIALOGTHREAD_COMPLETED, wxCommandEventHandler( WaitDialog::OnThreadCompletion ) );
   70             Disconnect( wxID_ANY, wxEVT_COMMAND_WAITDIALOG_ADMIN_PASSWORD, wxCommandEventHandler( WaitDialog::OnAdminPasswordRequest ) );
   71             Disconnect( wxID_ANY, wxEVT_COMMAND_WAITDIALOG_PIN, wxCommandEventHandler( WaitDialog::OnPinRequest ) );
   72             Disconnect( wxID_ANY, wxEVT_COMMAND_WAITDIALOG_SHOW_MSG, wxCommandEventHandler( WaitDialog::OnShowMsg ) );
   73         }
   74 
   75         virtual void OnWaitDialogInit( wxInitDialogEvent& event )
   76         {
   77             m_thread->Run();
   78             m_timer.Start(100);
   79             m_bThreadRunning = true;
   80         }
   81 
   82         int GetCharWidth (wxWindow *window) const
   83         {
   84             int width;
   85             int height;
   86             window->GetTextExtent (L"a", &width, &height);
   87 
   88             if (width < 1)
   89                 return 7;
   90 
   91             return width;
   92         }
   93 
   94         class ShowMessageParam
   95         {
   96         public:
   97             wxString m_message;
   98             wxString m_caption;
   99             long m_style;
  100             bool m_topMost;
  101             ShowMessageParam(const wxString &message, const wxString &caption,long style, bool topMost)
  102                 : m_message(message), m_caption(caption), m_style(style), m_topMost(topMost)
  103             {}
  104         };
  105 
  106         int RequestShowMessage (const wxString &message, const wxString &caption,long style, bool topMost)
  107         {
  108             long lResult = -1;
  109             if (m_queue.IsOk())
  110             {
  111                 wxString sResult;
  112                 ShowMessageParam* pParam = new ShowMessageParam(message, caption, style, topMost);
  113                 wxCommandEvent* pEvent = new wxCommandEvent( wxEVT_COMMAND_WAITDIALOG_SHOW_MSG,0);
  114                 pEvent->SetClientData (pParam);
  115                 wxQueueEvent (this, pEvent);
  116                 m_queue.Receive (sResult);
  117                 sResult.ToLong(&lResult);
  118             }
  119             return (int) lResult;
  120         }
  121 
  122         void RequestAdminPassword (wxString& adminPassword)
  123         {
  124             if (m_queue.IsOk())
  125             {
  126                 wxQueueEvent (this, new wxCommandEvent( wxEVT_COMMAND_WAITDIALOG_ADMIN_PASSWORD,0));
  127                 if (wxMSGQUEUE_NO_ERROR != m_queue.Receive (adminPassword))
  128                     adminPassword = wxT("");
  129             }
  130             else
  131                 adminPassword = wxT("");
  132         }
  133 
  134         void RequestPin (wxString& pin)
  135         {
  136             if (m_queue.IsOk())
  137             {
  138                 wxCommandEvent* pEvent = new wxCommandEvent( wxEVT_COMMAND_WAITDIALOG_PIN,0);
  139                 pEvent->SetString (pin);
  140                 wxQueueEvent (this, pEvent);
  141                 if (wxMSGQUEUE_NO_ERROR != m_queue.Receive (pin))
  142                     pin = wxT("");
  143             }
  144             else
  145                 pin = wxT("");
  146         }
  147 
  148         virtual void OnWaitDialogClose( wxCloseEvent& event ) 
  149         { 
  150             if (event.CanVeto () && m_bThreadRunning)
  151             {
  152                 event.Veto ();
  153             }
  154             else
  155                 event.Skip ();
  156         }
  157  
  158         void OnThreadCompletion(wxCommandEvent &)
  159         {
  160             m_bThreadRunning = false;
  161             m_queue.Clear();
  162             EndModal(0);
  163         }
  164 
  165         void OnAdminPasswordRequest(wxCommandEvent &)
  166         {
  167 
  168             wxPasswordEntryDialog dialog (this, _("Enter your user password or administrator password:"), _("Administrator privileges required"));
  169             if (dialog.ShowModal() != wxID_OK)
  170                 m_queue.Post(wxT(""));
  171             else
  172                 m_queue.Post(dialog.GetValue());
  173         }
  174 
  175 
  176 
  177         void OnPinRequest(wxCommandEvent &e)
  178         {
  179 
  180             wxPasswordEntryDialog dialog (this, wxString::Format (LangString["ENTER_TOKEN_PASSWORD"], e.GetString()), LangString["IDD_TOKEN_PASSWORD"]);
  181             dialog.SetSize (wxSize (GetCharWidth (&dialog) * 50, -1));
  182 
  183             if (dialog.ShowModal() != wxID_OK)
  184                 m_queue.Post(wxT(""));
  185             else
  186                 m_queue.Post(dialog.GetValue());
  187         }
  188 
  189         void OnShowMsg(wxCommandEvent &e)
  190         {
  191             ShowMessageParam* pParam = (ShowMessageParam*) e.GetClientData();
  192             if (pParam->m_topMost)
  193             {
  194                 if (!IsActive())
  195                     RequestUserAttention (wxUSER_ATTENTION_ERROR);
  196 
  197                 pParam->m_style |= wxSTAY_ON_TOP;
  198             }
  199 
  200             int iResult = wxMessageBox (pParam->m_message, pParam->m_caption, pParam->m_style, this);
  201             delete pParam;
  202             m_queue.Post(wxString::Format(wxT("%d"), iResult));
  203         }
  204 
  205         void OnProgressTimer(wxTimerEvent& event)
  206         {
  207             WaitProgessBar->Pulse();
  208         }
  209 
  210         virtual void Run(void) { ShowModal(); if (m_pRoutine->HasException()) ThrowException(m_pRoutine->m_pException); }
  211 
  212         void ThrowException(Exception* ex);
  213 
  214     protected:
  215         WaitThread* m_thread;
  216         bool m_bThreadRunning;
  217         wxTimer m_timer;
  218         wxMessageQueue<wxString> m_queue;
  219     };
  220 }
  221 
  222 #endif // TC_HEADER_Main_Forms_WaitDialog