qthighway/xqserviceutil/src/xqrequestutil.cpp
changeset 26 3d09643def13
parent 8 71781823f776
equal deleted inserted replaced
24:9d760f716ca8 26:3d09643def13
    20 */
    20 */
    21 
    21 
    22 #include "xqservicelog.h"
    22 #include "xqservicelog.h"
    23 #include "xqserviceerrdefs.h"
    23 #include "xqserviceerrdefs.h"
    24 
    24 
       
    25 #include <w32std.h> // RWsSession
       
    26 #include <coemain.h>
       
    27 #include <QCoreApplication>
       
    28 
    25 #include "xqrequestutil.h"
    29 #include "xqrequestutil.h"
    26    
    30    
       
    31 // ======== LOCAL CLASSES ========
       
    32 
       
    33 class CParentObserver: public CActive
       
    34 {
       
    35 public:
       
    36 
       
    37     /**
       
    38      * Starts to observe if parent is still running and close embedded view when parent closed
       
    39      */
       
    40     static void closeWhenParentClosedL();
       
    41 
       
    42     /**
       
    43      * C++ destructor.
       
    44      */
       
    45     virtual ~CParentObserver();
       
    46         
       
    47 protected:
       
    48     
       
    49     // @see CActive
       
    50     virtual void RunL();
       
    51     
       
    52     // @see CActive
       
    53     virtual void DoCancel(); 
       
    54     
       
    55     // @see CActive
       
    56     virtual TInt RunError( TInt aError ); 
       
    57     
       
    58 private:
       
    59 
       
    60     CParentObserver();
       
    61     void ConstructL();
       
    62     void IssueRequest();
       
    63     void HandleWsEventL(const TWsEvent &aEvent);
       
    64 
       
    65 private: // Data
       
    66 
       
    67     class ParentObserverDestructor
       
    68     {
       
    69     public:
       
    70         ParentObserverDestructor():iObserver(NULL){}
       
    71         
       
    72         ~ParentObserverDestructor()
       
    73         {
       
    74             delete iObserver;
       
    75         }
       
    76         
       
    77         CParentObserver* iObserver;
       
    78     };
       
    79     
       
    80     static ParentObserverDestructor iInstance;
       
    81     
       
    82     /** Window server session */
       
    83     RWsSession iWsSession;
       
    84     
       
    85     /** Window group for receiving window server events */
       
    86     RWindowGroup* iWindowGroup;
       
    87 };
       
    88 
       
    89 CParentObserver::ParentObserverDestructor CParentObserver::iInstance;
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // C++ constructor.
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 CParentObserver::CParentObserver():
       
    96     CActive(CActive::EPriorityStandard)
       
    97 {
       
    98     CActiveScheduler::Add(this);
       
    99 }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // Symbian 2nd phase constructor.
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CParentObserver::ConstructL()
       
   106 {
       
   107     // Connect to window server server
       
   108     User::LeaveIfError(iWsSession.Connect());
       
   109     
       
   110     // Construct window group
       
   111     iWindowGroup = new(ELeave) RWindowGroup(iWsSession);
       
   112     User::LeaveIfError( iWindowGroup->Construct((TUint32)this, EFalse));
       
   113     User::LeaveIfError(iWindowGroup->EnableGroupChangeEvents());
       
   114 }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Starts to observe if parent is still running and close embedded view when parent closed
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void CParentObserver::closeWhenParentClosedL()
       
   121 {
       
   122     if (!iInstance.iObserver) {
       
   123         iInstance.iObserver = new (ELeave) CParentObserver();
       
   124         iInstance.iObserver->ConstructL();
       
   125     }
       
   126     
       
   127     iInstance.iObserver->IssueRequest();
       
   128 }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // C++ destructor.
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 CParentObserver::~CParentObserver()
       
   135 {
       
   136     Cancel();
       
   137     
       
   138     // Cleanup window group
       
   139     if(iWindowGroup) {
       
   140         iWindowGroup->DisableGroupChangeEvents();
       
   141         iWindowGroup->Close();
       
   142         delete iWindowGroup;
       
   143     }
       
   144     
       
   145     // Cleanup window server session
       
   146     iWsSession.Close();
       
   147 }
       
   148 
       
   149 //------------------------------------------------------------------------------
       
   150 // CParentObserver::IssueRequest
       
   151 //------------------------------------------------------------------------------
       
   152 //
       
   153 void CParentObserver::IssueRequest()
       
   154 {
       
   155     if(!IsActive()) {
       
   156         // Request events from window server
       
   157         iWsSession.EventReady(&iStatus);
       
   158         SetActive();
       
   159     }
       
   160 }
       
   161 
       
   162 //------------------------------------------------------------------------------
       
   163 // CParentObserver::HandleWsEventL
       
   164 //------------------------------------------------------------------------------
       
   165 //
       
   166 void CParentObserver::HandleWsEventL(const TWsEvent &aEvent)
       
   167 {
       
   168     if (aEvent.Type() == EEventWindowGroupsChanged) {
       
   169         RWindowGroup rootWG = CCoeEnv::Static()->RootWin();
       
   170         int rootWGId = rootWG.Identifier();
       
   171         
       
   172         RArray<RWsSession::TWindowGroupChainInfo> list;
       
   173         CleanupClosePushL(list);
       
   174         iWsSession.WindowGroupList(&list);
       
   175         
       
   176         for(int idx = 0; idx < list.Count(); idx++)
       
   177         {
       
   178             RWsSession::TWindowGroupChainInfo info = list[idx];
       
   179             if (info.iId == rootWGId && info.iParentId <= 0) {
       
   180                qApp->quit();
       
   181                break;
       
   182             }
       
   183         }
       
   184         CleanupStack::PopAndDestroy(); // list
       
   185     }
       
   186 }
       
   187 
       
   188 //------------------------------------------------------------------------------
       
   189 // CParentObserver::RunL
       
   190 //------------------------------------------------------------------------------
       
   191 //
       
   192 void CParentObserver::RunL()
       
   193 {
       
   194     TInt err = iStatus.Int();
       
   195     if(err == KErrNone) {
       
   196         // No errors occured, fetch event
       
   197         TWsEvent wsEvent;
       
   198         iWsSession.GetEvent(wsEvent);
       
   199         
       
   200         // Continue listening
       
   201         IssueRequest();
       
   202         
       
   203         // Forward event to observer
       
   204         HandleWsEventL(wsEvent);
       
   205     }
       
   206     else {
       
   207         // Continue listening
       
   208         IssueRequest();
       
   209     }    
       
   210 }
       
   211 
       
   212 //------------------------------------------------------------------------------
       
   213 // CParentObserver::DoCancel
       
   214 //------------------------------------------------------------------------------
       
   215 //
       
   216 void CParentObserver::DoCancel()
       
   217 {
       
   218     // Cancel event ready from window server
       
   219     iWsSession.EventReadyCancel();
       
   220 }
       
   221 
       
   222 //------------------------------------------------------------------------------
       
   223 // CParentObserver::RunError
       
   224 //------------------------------------------------------------------------------
       
   225 //
       
   226 TInt CParentObserver::RunError(TInt /*aError*/)
       
   227 {
       
   228     // Issue a new request
       
   229     IssueRequest();
       
   230     
       
   231     return KErrNone;
       
   232 }
       
   233 
       
   234 /*!
       
   235     Observes if client is still running and close embedded view when client closed
       
   236 */
       
   237 void XQServiceUtils::closeWhenClientClosed()
       
   238 {
       
   239     XQSERVICE_DEBUG_PRINT("XQServiceUtil::closeEVWhenClientClosed");
       
   240     
       
   241     TRAPD(err, CParentObserver::closeWhenParentClosedL());
       
   242     
       
   243     XQSERVICE_DEBUG_PRINT("XQServiceUtil::closeEVWhenClientClosed return value: ", err);
       
   244 }
       
   245 
    27 XQRequestUtil::XQRequestUtil()
   246 XQRequestUtil::XQRequestUtil()
    28 {
   247 {
    29     XQSERVICE_DEBUG_PRINT("XQRequestUtil::XQRequestUtil");
   248     XQSERVICE_DEBUG_PRINT("XQRequestUtil::XQRequestUtil");
    30 }
   249 }
    31 
   250