zeroconf/zeroconfsharing/downloadicon.cpp
changeset 21 ff5174af067c
equal deleted inserted replaced
14:da856f45b798 21:ff5174af067c
       
     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 "downloadicon.h"
       
    19 
       
    20 #include <QtNetwork>
       
    21 #include <QNetworkRequest>
       
    22 #include <QNetworkReply>
       
    23 #include <QMessageBox>
       
    24 
       
    25 DownloadIcon::DownloadIcon(Ui::MainWindow &aMainUi)
       
    26     :MainUi(aMainUi)
       
    27     {
       
    28     manager = new QNetworkAccessManager(this);
       
    29     
       
    30     }
       
    31 DownloadIcon::~DownloadIcon()
       
    32     {
       
    33     if(manager)
       
    34         {
       
    35         manager->disconnect();    
       
    36         delete manager;
       
    37         manager = 0;
       
    38         }
       
    39     }
       
    40 
       
    41 void DownloadIcon::ChangePathSlash()
       
    42     {
       
    43     //filePath contains C:\\Data\\ZeroConf\\name.ico
       
    44     //after this method is called it must have
       
    45     //c://data//zeroconf//name.ico
       
    46     int start = 0;
       
    47     while( (start = filePath.indexOf("\\",start))!= -1)
       
    48         {
       
    49             filePath.remove(start,1);
       
    50             filePath.insert(start,"//");
       
    51         }
       
    52     }
       
    53 
       
    54 
       
    55 void DownloadIcon::UpdateAvatarItem()
       
    56     {
       
    57     int count = MainUi.listAvatars->count();
       
    58     int index = 0;
       
    59     while(index < count)
       
    60         {
       
    61         QListWidgetItem* item = MainUi.listAvatars->item(index);
       
    62         if(contactName.compare(item->text()) == 0)
       
    63             {
       
    64             QPixmap pm;
       
    65             //ChangePathSlash();
       
    66             //DisplayMsgBox(filePath);
       
    67             
       
    68             //if(pm.load(filePath))
       
    69             if(pm.load("E://mypic.ico"))
       
    70                 {
       
    71                 QIcon icon(pm);
       
    72                 DisplayMsgBox(filePath);
       
    73                 item->setIcon(icon);
       
    74                 }
       
    75             break;
       
    76             }
       
    77         index++;
       
    78         }
       
    79     
       
    80     }
       
    81 
       
    82 
       
    83 void DownloadIcon::RequestFinished()
       
    84     {
       
    85     
       
    86     DisplayMsgBox(tr("Got Response"));
       
    87     
       
    88     int error = reply->error();
       
    89     if(!error)
       
    90         {
       
    91         QNetworkReply *reply = ((QNetworkReply *)sender());
       
    92         QByteArray data = reply->readAll();
       
    93         
       
    94         if(data.isEmpty())
       
    95             DisplayMsgBox(tr("No Data Reply"));
       
    96         
       
    97         //QTextStream out(&data);
       
    98         //QString content = out.readAll();
       
    99         
       
   100         //QMessageBox::warning(this, tr("Content"),content,QMessageBox::Ok);
       
   101         
       
   102         //QFile temp(filePath);
       
   103         QFile temp("E:\\mypic.ico");
       
   104         temp.open(QIODevice::WriteOnly | QIODevice::Truncate);
       
   105         temp.write(data);        
       
   106         temp.close();
       
   107         UpdateAvatarItem();
       
   108         }
       
   109     else
       
   110         DisplayMsgBox(reply->errorString());
       
   111     
       
   112     //Delete this object once the response is received.
       
   113     delete this;
       
   114     
       
   115     }
       
   116 
       
   117 
       
   118 void DownloadIcon::GetIcon(const TDesC8& aContactName, const TDesC8& aUri, const TDesC8& aFileName)
       
   119     {
       
   120     
       
   121     QString quri = QString::fromUtf8((const char*)aUri.Ptr(),aUri.Length()); 
       
   122     
       
   123     contactName = QString::fromUtf8((const char*)aContactName.Ptr(),aContactName.Length());
       
   124     filePath = QString::fromUtf8((const char*)aFileName.Ptr(),aFileName.Length());
       
   125     
       
   126     //DisplayMsgBox(quri);
       
   127     
       
   128 #ifdef __arm 
       
   129     reply = manager->get(QNetworkRequest(quri));
       
   130 #else
       
   131     reply = manager->get(QNetworkRequest(QUrl("http://kiranp747.googlepages.com/FileList.txt")));
       
   132 #endif
       
   133 
       
   134     
       
   135     connect(reply, SIGNAL(finished()), this, SLOT(RequestFinished()));
       
   136 
       
   137     }
       
   138 
       
   139 void DownloadIcon::DisplayMsgBox(QString str)
       
   140     {
       
   141     QMessageBox msgBox;
       
   142     msgBox.setText(str);
       
   143     msgBox.exec();
       
   144     }
       
   145 
       
   146 
       
   147 
       
   148