presetserver/clientsrc/Psserv.cpp
changeset 0 09774dfdd46b
child 12 608f67c22514
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2006-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Preset server client implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <pspresetinterface.h>
       
    20 #include <psserv.h>
       
    21 
       
    22 // ======== LOCAL FUNCTIONS ========
       
    23 
       
    24 namespace 
       
    25     {
       
    26 
       
    27     // ---------------------------------------------------------------------------
       
    28     // Panics the application with the supplied reason.
       
    29     // ---------------------------------------------------------------------------
       
    30     //
       
    31     void Panic( TInt aReason )
       
    32         {
       
    33         _LIT( panic, "PSServ" );
       
    34         User::Panic( panic, aReason );
       
    35         }
       
    36 
       
    37     // ---------------------------------------------------------------------------
       
    38     // Checks whether or not the server is running.
       
    39     // ---------------------------------------------------------------------------
       
    40     //
       
    41     TBool IsServerStarted()
       
    42         {
       
    43         TFindServer server( KPSServerName );
       
    44         TFullName serverName;
       
    45         return ( server.Next( serverName ) == KErrNone );
       
    46         }
       
    47     
       
    48     // ---------------------------------------------------------------------------
       
    49     // Starts the server.
       
    50     // ---------------------------------------------------------------------------
       
    51     //
       
    52     TInt StartServer()
       
    53         {
       
    54         TInt result = KErrNone;
       
    55 
       
    56         if ( !IsServerStarted() )
       
    57             {
       
    58             RProcess process;
       
    59             result = process.Create( KPSServerExe, KNullDesC );
       
    60 
       
    61             if ( result == KErrNone || result == KErrAlreadyExists )
       
    62                 {
       
    63                 TRequestStatus logonStatus;
       
    64                 TRequestStatus rendezvousStatus;
       
    65                 
       
    66                 process.Logon( logonStatus );
       
    67                 process.Rendezvous( rendezvousStatus );
       
    68                 
       
    69                 // start process
       
    70                 if ( result == KErrNone )
       
    71                     {
       
    72                     process.Resume();
       
    73                     }
       
    74                 
       
    75                 // wait for either of statuses to be signaled
       
    76                 User::WaitForRequest( logonStatus, rendezvousStatus );
       
    77 
       
    78                 // if logonStatus was not signaled (it is still KRequestPending)
       
    79                 // cancel it
       
    80                 if ( logonStatus == KRequestPending ) // Server succesfully started.
       
    81                     {
       
    82                     process.LogonCancel( logonStatus );
       
    83                     User::WaitForRequest( logonStatus );
       
    84                     }
       
    85                 else
       
    86                     {
       
    87                     process.RendezvousCancel( rendezvousStatus );
       
    88                     User::WaitForRequest( rendezvousStatus );
       
    89                     }
       
    90                 }
       
    91 
       
    92             __ASSERT_ALWAYS( IsServerStarted(), Panic( KErrNotFound ) );
       
    93             }
       
    94 
       
    95         return result;
       
    96         }
       
    97 
       
    98     // ---------------------------------------------------------------------------
       
    99     // Returns the current version number.
       
   100     // ---------------------------------------------------------------------------
       
   101     //
       
   102     TVersion Version()
       
   103         {
       
   104         return TVersion( KPSVersionMajor, KPSVersionMinor, KPSVersionBuild );
       
   105         }
       
   106 
       
   107     }
       
   108 
       
   109 // ======== MEMBER FUNCTIONS ========
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Constructor.
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 EXPORT_C RPSServ::RPSServ()
       
   116     : RSessionBase(), iReserved()
       
   117     {
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // Connects the session to the server.
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 EXPORT_C TInt RPSServ::Connect()
       
   125     {
       
   126     StartServer();
       
   127     
       
   128     return CreateSession( KPSServerName, Version() );
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // Closes the session.
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 EXPORT_C void RPSServ::Close()
       
   136     {
       
   137     RHandleBase::Close();
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Creates a preset.
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 EXPORT_C CPSPresetInterface* RPSServ::CreatePresetL( TInt aIndex, TUid aDataHandler )
       
   145     {
       
   146     TPckgBuf<TInt> id;
       
   147     User::LeaveIfError( SendReceive( EPSOpCreatePreset, TIpcArgs( aIndex, aDataHandler.iUid, &id ) ) );   
       
   148     
       
   149     return CPSPresetInterface::CreateL( *this, id(), aDataHandler );
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Opens a preset.
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C CPSPresetInterface* RPSServ::OpenPresetL( TInt aId )
       
   157     {
       
   158     TPckgBuf<TInt> dataHandler;
       
   159     User::LeaveIfError( SendReceive( EPSOpGetPresetDataHandler, TIpcArgs( aId, &dataHandler ) ) );
       
   160 
       
   161     return CPSPresetInterface::CreateL( *this, aId, TUid::Uid( dataHandler() ) );
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // Deletes a preset.
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 EXPORT_C void RPSServ::DeletePresetL( TInt aId )
       
   169     {
       
   170     User::LeaveIfError( SendReceive( EPSOpDeletePreset, TIpcArgs( aId ) ) );
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // Moves a preset.
       
   175 // ---------------------------------------------------------------------------
       
   176 //
       
   177 EXPORT_C void RPSServ::MovePresetL( TInt aId, TInt aDestinationIndex )
       
   178     {
       
   179     User::LeaveIfError( SendReceive( EPSOpMovePreset, TIpcArgs( aId, aDestinationIndex) ) );
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Gets all presets.
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C void RPSServ::GetPresetsL( RPointerArray<CPSPresetInterface>& aPresets )
       
   187     {
       
   188     GetPresetsL<CPSPresetInterface>( aPresets, KNullUid );
       
   189     }
       
   190 
       
   191 // ======== GLOBAL FUNCTIONS ========