ginebra/chromejsobject.cpp
changeset 3 0954f5dd2cd0
parent 1 b0dd75e285d2
child 4 d5cdb6bc139d
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "chromejsobject.h"
       
    19 #include "qwebpage.h"
       
    20 #include "qwebframe.h"
       
    21 #include "chromewidget.h"
       
    22 #include "chromeview.h"
       
    23 #include "webcontentview.h"
       
    24 #include <QNetworkReply>
       
    25 #include "scriptobjects.h"
       
    26 #include "UiUtil.h"
       
    27 #include "webpagecontroller.h"
       
    28 #include <qdesktopservices.h>
       
    29 
       
    30 #ifdef USE_DOWNLOAD_MANAGER
       
    31 #include "download.h"
       
    32 #endif
       
    33 
       
    34 ChromeJSObject::ChromeJSObject(ChromeView* chrome)
       
    35  : m_chromeView(chrome)
       
    36 {
       
    37   setObjectName("chrome");
       
    38 #ifdef ENABLE_LOG
       
    39   initLogFile();
       
    40 #endif
       
    41 }
       
    42 
       
    43 void ChromeJSObject::loadChrome(const QString url)  // slot
       
    44 {
       
    45   m_chromeView->loadChrome(url);
       
    46 }
       
    47 
       
    48 void ChromeJSObject::reloadChrome()  // slot
       
    49 {
       
    50   m_chromeView->reloadChrome();
       
    51 }
       
    52 
       
    53 #ifdef USE_STATEMACHINE
       
    54 void ChromeJSObject::requestChrome(const QString &url)  // slot
       
    55 {
       
    56   qDebug() << "ChromeJSObject::requestChrome: " << url;
       
    57   emit onRequestChrome(url);
       
    58 }
       
    59 #endif
       
    60 
       
    61 void ChromeJSObject::updateViewPort(){
       
    62   m_chromeView->updateViewPort();
       
    63 }
       
    64 
       
    65 void ChromeJSObject::setViewPort(int x1, int y1, int x2, int y2){
       
    66   QRect viewPort(x1,y1,x2,y2);
       
    67   m_chromeView->setViewPort(viewPort);
       
    68 }
       
    69 void ChromeJSObject::flipFromCurrentView(const QString& toId){
       
    70   m_chromeView->flipFromCurrentView(toId);
       
    71 }
       
    72 
       
    73 void ChromeJSObject::flipToCurrentView(const QString& fromId){
       
    74   m_chromeView->flipToCurrentView(fromId);
       
    75 }
       
    76 
       
    77 void ChromeJSObject::setLocation(const QString& id, int x, int y){
       
    78   m_chromeView->setLocation(id,x,y);
       
    79 }
       
    80 
       
    81 void ChromeJSObject::setAnchor(const QString& id, const QString& anchor){
       
    82   m_chromeView->setAnchor(id, anchor);
       
    83 }
       
    84 
       
    85 void ChromeJSObject::toggleVisibility(const QString& id){
       
    86   m_chromeView->toggleVisibility(id);
       
    87 }
       
    88 
       
    89 void ChromeJSObject::show(const QString& id, int x, int y){
       
    90   m_chromeView->show(id, x, y);
       
    91 }
       
    92 
       
    93 void ChromeJSObject::hide(const QString& id){
       
    94   m_chromeView->hide(id);
       
    95 }
       
    96 
       
    97 
       
    98 void ChromeJSObject::toggleAttention(const QString& id){
       
    99   m_chromeView->toggleAttention(id);
       
   100 
       
   101 }
       
   102 
       
   103 void ChromeJSObject::setVisibilityAnimator(const QString& elementId, const QString & animatorName){
       
   104   m_chromeView->setVisibilityAnimator(elementId, animatorName);
       
   105 }
       
   106 
       
   107 void ChromeJSObject::setAttentionAnimator(const QString& elementId, const QString & animatorName){
       
   108   qDebug() << "Set attention animator";
       
   109   m_chromeView->setAttentionAnimator(elementId, animatorName);
       
   110 }
       
   111 
       
   112 QString ChromeJSObject::guessUrlFromString(const QString &s){
       
   113     QUrl u = WRT::UiUtil::guessUrlFromString(s);
       
   114     return u.toString();
       
   115 }
       
   116 
       
   117 QString ChromeJSObject::guessAndGotoUrl(const QString &s){
       
   118     QString guessedUrl(guessUrlFromString(s));
       
   119     WebPageController::getSingleton()->currentLoad(guessedUrl);
       
   120     return guessedUrl;
       
   121 }
       
   122 
       
   123 QString ChromeJSObject::searchUrl(const QString &s){
       
   124     QUrl u = WRT::UiUtil::searchUrl(s);
       
   125     return u.toString();
       
   126 }
       
   127 
       
   128 void ChromeJSObject::loadFromHistory()
       
   129 {
       
   130     int count = WebPageController::getSingleton()->historyWindowCount();
       
   131         
       
   132     if(!count)
       
   133     {
       
   134     		loadLocalFile(); // load start page for first startup of the browser
       
   135     }
       
   136     else
       
   137     {	
       
   138     	 int count2 = WebPageController::getSingleton()->restoreNumberOfWindows();
       
   139     	 
       
   140     	 // "sanity check" count shall be always equal to count2. If not, something wrong. Open only one window - start page in this case.
       
   141     	 	
       
   142     	 if((count > 0) && (count == count2)) // load all browser windows from the last session
       
   143     	 {
       
   144     	 	
       
   145     	 	for(int i = 0; i < count2-1; i++) //all windows except current
       
   146     	 	{
       
   147     	 		WebPageController::getSingleton()->openPageFromHistory(i);
       
   148     	 		WebPageController::getSingleton()->pageGotoCurrentItem(i);
       
   149     	 		WebPageController::getSingleton()->pageReload(i);
       
   150     	 	}		
       
   151     	 
       
   152     	 // current window
       
   153     	 WebPageController::getSingleton()->currentLoad("");	
       
   154     	 WebPageController::getSingleton()->gotoCurrentItem();
       
   155     	 WebPageController::getSingleton()->currentReload();	
       
   156     	     	 		
       
   157     	 WebPageController::getSingleton()->deleteHistory();
       
   158     	 	
       
   159     	 }
       
   160     	 else // browser did not close correctly; exception handler: delete all files and load start page as in first startup
       
   161     	 {
       
   162     	 	WebPageController::getSingleton()->deleteHistory();
       
   163     	 	WebPageController::getSingleton()->deleteDataFiles();
       
   164     	 	loadLocalFile();
       
   165     	 }			    	 
       
   166     }	 
       
   167     
       
   168     return ;
       
   169 }
       
   170 
       
   171 	
       
   172 	
       
   173 QString ChromeJSObject::getLastUrl()
       
   174 {
       
   175 	QString lastUrl = WebPageController::getSingleton()->getLastUrl();
       
   176 	if(lastUrl == "")
       
   177 		return "http://";
       
   178 	else
       
   179 		return lastUrl ;	
       
   180 }
       
   181 
       
   182 int ChromeJSObject::numberOfWindows()
       
   183 {
       
   184 	int numWin = WebPageController::getSingleton()->restoreNumberOfWindows();
       
   185 	
       
   186 	return numWin;
       
   187 }
       
   188 
       
   189 void ChromeJSObject::clearHistory()
       
   190 {
       
   191 	WebPageController::getSingleton()->deleteHistory();
       
   192 	WebPageController::getSingleton()->clearHistoryInMemory();
       
   193 	WebPageController::getSingleton()->deleteDataFiles();
       
   194 }
       
   195 
       
   196 void ChromeJSObject::clearCookies()
       
   197 {
       
   198 	WebPageController::getSingleton()->deleteCookies();
       
   199 }
       
   200 
       
   201 void ChromeJSObject::clearCache()
       
   202 {
       
   203 	WebPageController::getSingleton()->deleteCache();
       
   204 }
       
   205 
       
   206 void ChromeJSObject::loadLocalFile() {
       
   207 // for symbian deployment, local file root at C:/data/Others/
       
   208     QString chromeBaseDir = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString("ChromeBaseDirectory");
       
   209     QString startPage = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString("StartPage");
       
   210     QString startPagePath = chromeBaseDir + startPage;
       
   211     qDebug() << "ChromeJSObject::loadLocalFile: " << startPagePath;
       
   212     WebPageController::getSingleton()->currentLoad(startPagePath);
       
   213 }
       
   214 
       
   215 void ChromeJSObject::setViewofCurrentPage() {
       
   216     m_chromeView->setViewofCurrentPage();
       
   217 }
       
   218 
       
   219 QObject *ChromeJSObject::getContentViewRect() const {
       
   220     ScriptRect *rect = new ScriptRect(m_chromeView->geometry());
       
   221     m_chromeView->getChromeWidget()->chromePage()->mainFrame()->addToJavaScriptWindowObject(rect->objectName(), rect, QScriptEngine::ScriptOwnership);
       
   222     return rect;
       
   223 }
       
   224 
       
   225 void ChromeJSObject::alert(const QString msg) {
       
   226   // To do: open a dialog box showing msg.
       
   227   qDebug() << msg;
       
   228 }
       
   229 
       
   230 #ifdef ENABLE_LOG
       
   231 
       
   232 
       
   233 QString ChromeJSObject::getLogPath()
       
   234 {
       
   235 #if defined(QT_NO_DESKTOPSERVICES)
       
   236 #ifdef Q_OS_SYMBIAN
       
   237 	static const QString LogPath("C:\\Data\\GinebraLog.txt");
       
   238 #else
       
   239 	static const QString LogPath("./GinebraLog.txt");
       
   240 #endif
       
   241 #else
       
   242     static const QString LogPath(QDesktopServices::storageLocation(QDesktopServices::TempLocation) + "./GinebraLog.txt");
       
   243 #endif
       
   244     return LogPath;
       
   245 }
       
   246 
       
   247 void ChromeJSObject::initLogFile() {
       
   248     QFile file(getLogPath());
       
   249     file.remove();
       
   250     log("Log started " + QDate::currentDate().toString("dd.MM.yyyy"));
       
   251 }
       
   252 
       
   253 void ChromeJSObject::log(const QString msg) {
       
   254     QFile file(getLogPath());
       
   255     
       
   256     if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
       
   257         qDebug() << "ChromeJSObject::log: open failed";
       
   258         return;
       
   259     }
       
   260     
       
   261     QTextStream out(&file);
       
   262     out << "\r\n" << QTime::currentTime ().toString("hh:mm:ss.zzz: ") << msg;
       
   263     
       
   264     file.flush();
       
   265     file.close();
       
   266 }
       
   267 #endif   // ENABLE_LOG
       
   268 
       
   269 void ChromeJSObject::setBackgroundColor(int r, int g, int b){
       
   270     m_chromeView->setBackgroundColor(QColor(r, g, b));
       
   271 }
       
   272 
       
   273 void ChromeJSObject::displayModeChanged(ChromeView::DisplayMode displayMode){
       
   274     emit onDisplayModeChanged(displayMode == ChromeView::DisplayModePortrait ? "portrait" : "landscape");
       
   275 }
       
   276 
       
   277 void ChromeJSObject::displayModeChangeStart(ChromeView::DisplayMode displayMode) {
       
   278 
       
   279     emit onDisplayModeChangeStart(displayMode == ChromeView::DisplayModePortrait ? "portrait" : "landscape");
       
   280 }
       
   281 
       
   282 //-------------------------------------
       
   283 // Property getters and setters
       
   284 //-------------------------------------
       
   285 
       
   286 QList<int> ChromeJSObject::getDisplaySize() const
       
   287 {
       
   288     QList<int> list;
       
   289     list.append(m_chromeView->size().width());
       
   290     list.append(m_chromeView->size().height());
       
   291     return list;
       
   292 }
       
   293 
       
   294 QString ChromeJSObject::getDisplayMode() const{
       
   295     return m_chromeView->displayMode() == ChromeView::DisplayModePortrait ? "portrait" : "landscape";
       
   296 }
       
   297 /*
       
   298 int ChromeJSObject::getDisplayOrientation() const{
       
   299     return m_chromeView->orientationAngle();
       
   300 }
       
   301 */
       
   302 
       
   303 // Support downloads
       
   304 
       
   305 #ifdef USE_DOWNLOAD_MANAGER
       
   306 
       
   307 static int dlInt(Download * download, DownloadAttribute attribute)
       
   308 {
       
   309     return download->getAttribute(attribute).toInt();
       
   310 }
       
   311 
       
   312 static QString dlStr(Download * download, DownloadAttribute attribute)
       
   313 {
       
   314     return download->getAttribute(attribute).toString();
       
   315 }
       
   316 
       
   317 void ChromeJSObject::downloadCreated(Download * download)
       
   318 {
       
   319     // Note: the DlFileName at this point has not been
       
   320     // adjusted to account for duplicate file names in
       
   321     // the destination directory.  That doesn't happen
       
   322     // until the download starts.
       
   323 
       
   324     emit onDownloadCreated(download->id());
       
   325 }
       
   326 
       
   327 void ChromeJSObject::downloadStarted(Download * download)
       
   328 {
       
   329     emit onDownloadStarted(
       
   330             download->id(),
       
   331             dlStr(download, DlFileName),
       
   332             dlInt(download, DlTotalSize));
       
   333 }
       
   334 
       
   335 void ChromeJSObject::downloadProgress(Download * download)
       
   336 {
       
   337     emit onDownloadProgress(
       
   338             download->id(),
       
   339             dlStr(download, DlFileName),
       
   340             dlInt(download, DlTotalSize),
       
   341             dlInt(download, DlDownloadedSize),
       
   342             dlInt(download, DlPercentage));
       
   343 }
       
   344 
       
   345 void ChromeJSObject::downloadFinished(Download * download)
       
   346 {
       
   347     emit onDownloadFinished(
       
   348             download->id(),
       
   349             dlStr(download, DlFileName),
       
   350             dlInt(download, DlTotalSize),
       
   351             dlInt(download, DlDownloadedSize),
       
   352             dlInt(download, DlPercentage));
       
   353 }
       
   354 
       
   355 void ChromeJSObject::downloadPaused(Download * download, const QString & error)
       
   356 {
       
   357     emit onDownloadPaused(
       
   358             download->id(),
       
   359             dlStr(download, DlFileName),
       
   360             dlInt(download, DlTotalSize),
       
   361             dlInt(download, DlDownloadedSize),
       
   362             dlInt(download, DlPercentage),
       
   363             error);
       
   364 }
       
   365 
       
   366 void ChromeJSObject::downloadCancelled(Download * download, const QString & error)
       
   367 {
       
   368     emit onDownloadCancelled(
       
   369             download->id(),
       
   370             dlStr(download, DlFileName),
       
   371             dlInt(download, DlTotalSize),
       
   372             dlInt(download, DlDownloadedSize),
       
   373             dlInt(download, DlPercentage),
       
   374             error);
       
   375 }
       
   376 
       
   377 void ChromeJSObject::downloadFailed(Download * download, const QString & error)
       
   378 {
       
   379     emit onDownloadFailed(
       
   380             download->id(),
       
   381             dlStr(download, DlFileName),
       
   382             dlInt(download, DlTotalSize),
       
   383             dlInt(download, DlDownloadedSize),
       
   384             dlInt(download, DlPercentage),
       
   385             error);
       
   386 }
       
   387 
       
   388 void ChromeJSObject::downloadNetworkLoss(Download * download, const QString & error)
       
   389 {
       
   390     emit onDownloadNetworkLoss(
       
   391             download->id(),
       
   392             dlStr(download, DlFileName),
       
   393             dlInt(download, DlTotalSize),
       
   394             dlInt(download, DlDownloadedSize),
       
   395             dlInt(download, DlPercentage),
       
   396             error);
       
   397 }
       
   398 
       
   399 void ChromeJSObject::downloadError(Download * download, const QString & error)
       
   400 {
       
   401     emit onDownloadError(
       
   402             download->id(),
       
   403             dlStr(download, DlFileName),
       
   404             dlInt(download, DlTotalSize),
       
   405             dlInt(download, DlDownloadedSize),
       
   406             dlInt(download, DlPercentage),
       
   407             error);
       
   408 }
       
   409 
       
   410 #endif // USE_DOWNLOAD_MANAGER