Back to index

lightning-sunbird  0.9+nobinonly
nsMinimoDesktop.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Minimo Desktop
00015  *
00016  * The Initial Developer of the Original Code is 
00017  * Douglas F. Turner II  <dougt@meer.net>
00018  * Portions created by the Initial Developer are Copyright (C) 2006
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "nsGenericFactory.h"
00038 #include "nsIMinimoDesktop.h"
00039 #include "nsString.h"
00040 #include "nsIFile.h"
00041 
00042 #ifdef WIN32
00043 // You need to have the windows platform SDK in your INCLUDE path.
00044 #include <Objidl.h>
00045 #include <rapi.h>
00046 #endif
00047 
00048 class MinimoDesktop : public nsIMinimoDesktop
00049 {
00050 public:
00051   NS_DECL_ISUPPORTS
00052   NS_DECL_NSIMINIMODESKTOP
00053 
00054   MinimoDesktop();
00055 
00056 private:
00057   ~MinimoDesktop();
00058 };
00059 
00060 NS_IMPL_ISUPPORTS1(MinimoDesktop, nsIMinimoDesktop)
00061 
00062 PRBool device_connect()
00063 {
00064 #ifdef WIN32
00065   HRESULT hRapiResult = CeRapiInit();
00066   if (hRapiResult == E_FAIL)
00067     return PR_FALSE;
00068   return PR_TRUE;
00069 #else
00070   return PR_FALSE;
00071 #endif
00072 }
00073 
00074 void device_disconnect()
00075 {
00076   CeRapiUninit();
00077 }
00078 
00079 class device_connection
00080 {
00081 public:
00082   device_connection()
00083   {
00084     connected = device_connect();
00085   }
00086 
00087   ~device_connection()
00088   {
00089     device_disconnect();
00090   }
00091 
00092   PRBool connected;
00093 };
00094 
00095 MinimoDesktop::MinimoDesktop()
00096 {
00097 }
00098 
00099 MinimoDesktop::~MinimoDesktop()
00100 {
00101 }
00102 
00103 NS_IMETHODIMP MinimoDesktop::GetConnected(PRBool *aConnected)
00104 {
00105   device_connection dc;
00106   *aConnected = dc.connected;
00107   return NS_OK;
00108 }
00109 
00110 NS_IMETHODIMP MinimoDesktop::GetAvailableMemory(PRInt32 *aAvailableMemory)
00111 {
00112   device_connection dc;
00113   if (!dc.connected)
00114     return NS_ERROR_NOT_AVAILABLE;
00115 
00116 #ifdef WIN32
00117   MEMORYSTATUS ms;
00118   ms.dwLength = sizeof(MEMORYSTATUS);
00119 
00120   CeGlobalMemoryStatus(&ms);
00121 
00122   *aAvailableMemory = ms.dwAvailPhys;
00123 #endif
00124 
00125   return NS_OK;
00126 }
00127 
00128 NS_IMETHODIMP MinimoDesktop::GetBatteryLifePercent(PRInt32 *aBatteryLifePercent)
00129 {
00130   device_connection dc;
00131   if (!dc.connected)
00132     return NS_ERROR_NOT_AVAILABLE;
00133 
00134 #ifdef WIN32
00135   SYSTEM_POWER_STATUS_EX ps;
00136   BOOL b = CeGetSystemPowerStatusEx(&ps, false);
00137 
00138   if (!b)
00139     return NS_ERROR_FAILURE;
00140 
00141   *aBatteryLifePercent = ps.BatteryLifePercent;
00142 #endif
00143 
00144   return NS_OK;
00145 }
00146 
00147 NS_IMETHODIMP MinimoDesktop::GetMinimoInstallLocation(PRUnichar **_retval)
00148 {
00149   return NS_ERROR_NOT_IMPLEMENTED;
00150 }
00151 
00152 NS_IMETHODIMP MinimoDesktop::RemoteStartProcess(const PRUnichar *path, const PRUnichar *args)
00153 {
00154   device_connection dc;
00155   if (!dc.connected)
00156     return NS_ERROR_NOT_AVAILABLE;
00157 
00158 #ifdef WIN32
00159   PROCESS_INFORMATION p;
00160   CeCreateProcess(path, args, nsnull, nsnull, false, 0, nsnull, nsnull, nsnull, &p);
00161 
00162 #endif
00163 
00164   return NS_OK;
00165 }
00166 
00167 NS_IMETHODIMP MinimoDesktop::RemoteCreateDirectory(const PRUnichar *dir)
00168 {
00169   device_connection dc;
00170   if (!dc.connected)
00171     return NS_ERROR_NOT_AVAILABLE;
00172 
00173 #ifdef WIN32
00174   CeCreateDirectory(dir, nsnull); 
00175 #endif
00176 
00177   return NS_OK;
00178 }
00179 
00180 NS_IMETHODIMP MinimoDesktop::RemoteRemoveFile(const PRUnichar *source)
00181 {
00182   device_connection dc;
00183   if (!dc.connected)
00184     return NS_ERROR_NOT_AVAILABLE;
00185 
00186 #ifdef WIN32
00187   CeDeleteFile(source);
00188   CeRemoveDirectory(source);
00189 #endif
00190 
00191   return NS_OK;
00192 }
00193 
00194 NS_IMETHODIMP MinimoDesktop::RemoteCopyFile(const PRUnichar *source, const PRUnichar *dest)
00195 {
00196   device_connection dc;
00197   if (!dc.connected)
00198     return NS_ERROR_NOT_AVAILABLE;
00199 
00200 #ifdef WIN32
00201   CeCopyFile(source, dest, false);
00202 #endif
00203 
00204   return NS_OK;
00205 }
00206 
00207 NS_IMETHODIMP MinimoDesktop::RemoteMoveFile(const PRUnichar *source, const PRUnichar *dest)
00208 {
00209   device_connection dc;
00210   if (!dc.connected)
00211     return NS_ERROR_NOT_AVAILABLE;
00212 
00213 #ifdef WIN32
00214   CeMoveFile(source, dest);
00215 #endif
00216 
00217   return NS_OK;
00218 }
00219 
00220 NS_IMETHODIMP MinimoDesktop::CopyFileToDevice(const PRUnichar *locationOnDevice, nsIFile *localFile)
00221 {
00222   device_connection dc;
00223   if (!dc.connected)
00224     return NS_ERROR_NOT_AVAILABLE;
00225 
00226   nsresult rv = NS_OK;
00227 
00228   nsCAutoString path;
00229   localFile->GetNativePath(path);
00230 
00231   HANDLE hSrc = CreateFile(path.get(),
00232                            GENERIC_READ,
00233                            FILE_SHARE_READ,
00234                            NULL,
00235                            OPEN_EXISTING,
00236                            FILE_ATTRIBUTE_NORMAL,
00237                            NULL);
00238 
00239   if (INVALID_HANDLE_VALUE == hSrc)
00240   {
00241     return NS_ERROR_FAILURE;
00242   }
00243   
00244   HANDLE hDest = CeCreateFile(locationOnDevice,
00245                               GENERIC_WRITE,
00246                               FILE_SHARE_READ,
00247                               NULL,
00248                               CREATE_ALWAYS,
00249                               FILE_ATTRIBUTE_NORMAL,
00250                               NULL);
00251   
00252   if (INVALID_HANDLE_VALUE == hDest )
00253   {
00254     return NS_ERROR_FAILURE;
00255   }
00256 
00257   BYTE  buffer[4096];
00258   DWORD dwNumRead, dwNumWritten;
00259 
00260   do
00261   {
00262     if (ReadFile(hSrc, &buffer, sizeof(buffer), &dwNumRead, NULL))
00263     {
00264       if (!CeWriteFile(hDest, &buffer, dwNumRead, &dwNumWritten, NULL))
00265       {
00266         rv = NS_ERROR_FAILURE;
00267         goto FatalError;
00268       }
00269     }
00270     else
00271     {
00272       rv = NS_ERROR_FAILURE;
00273       goto FatalError;
00274     }
00275   }
00276   while (dwNumRead);
00277 
00278 FatalError:
00279     CeCloseHandle(hDest);
00280     CloseHandle (hSrc);
00281 
00282   return rv;
00283 }
00284 
00285 NS_IMETHODIMP MinimoDesktop::CopyFileFromDevice(const PRUnichar *locationOnDevice, nsIFile *localFile)
00286 {
00287   device_connection dc;
00288   if (!dc.connected)
00289     return NS_ERROR_NOT_AVAILABLE;
00290 
00291   nsresult rv = NS_OK;
00292 
00293   HANDLE hSrc = CeCreateFile(locationOnDevice,
00294                              GENERIC_READ,
00295                              FILE_SHARE_READ,
00296                              NULL,
00297                              OPEN_EXISTING,
00298                              FILE_ATTRIBUTE_NORMAL,
00299                              NULL);
00300 
00301   if (!hSrc)
00302     return NS_ERROR_FAILURE;
00303 
00304   nsCAutoString path;
00305   localFile->GetNativePath(path);
00306   
00307   HANDLE hDest = CreateFile(path.get(),
00308                             GENERIC_WRITE,
00309                             FILE_SHARE_READ,
00310                             NULL,
00311                             CREATE_ALWAYS,
00312                             FILE_ATTRIBUTE_NORMAL,
00313                             NULL);
00314 
00315   
00316   BYTE  buffer[4096];
00317   DWORD dwNumRead, dwNumWritten;
00318 
00319   do
00320   {
00321     if (CeReadFile( hSrc, &buffer, sizeof(buffer), &dwNumRead, NULL))
00322     {
00323      
00324       if (!WriteFile(hDest, &buffer, dwNumRead, &dwNumWritten, NULL))
00325       {
00326         rv = NS_ERROR_FAILURE;
00327         goto FatalError;
00328       }
00329     }
00330     else
00331     {
00332       rv = NS_ERROR_FAILURE;
00333       goto FatalError;
00334     }
00335   } 
00336   while (dwNumRead);
00337   
00338 FatalError:
00339   CeCloseHandle(hSrc);
00340   CloseHandle (hDest);
00341 
00342   return rv;
00343 }
00344 
00345 
00346 
00347 //------------------------------------------------------------------------------
00348 //  XPCOM REGISTRATION BELOW
00349 //------------------------------------------------------------------------------
00350 
00351 #define MinimoDesktop_CID \
00352 { 0x178e2513, 0x957c, 0x4e1b, \
00353 { 0x81, 0xf4, 0x11, 0x18, 0x35, 0x49, 0xc3, 0xd2 } }
00354 
00355 #define MinimoDesktop_ContractID "@mozilla.org/minimoDesktop;1"
00356 
00357 
00358 NS_GENERIC_FACTORY_CONSTRUCTOR(MinimoDesktop)
00359   
00360   
00361 static const nsModuleComponentInfo components[] =
00362 {
00363   { "MinimoDesktop", 
00364     MinimoDesktop_CID, 
00365     MinimoDesktop_ContractID,
00366     MinimoDesktopConstructor,
00367     nsnull,
00368     nsnull
00369   }
00370   
00371 };
00372 
00373 NS_IMPL_NSGETMODULE(MinimoDesktopModule, components)