zeroconf/zeroconfsharing/downloadicon.cpp
author guru.kini@nokia.com
Wed, 21 Jul 2010 14:28:48 +0530
changeset 21 ff5174af067c
permissions -rw-r--r--
ZeroConf Sharing Application
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     1
/*
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     3
* All rights reserved.
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     4
* This component and the accompanying materials are made available
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     6
* which accompanies this distribution, and is available
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     8
*
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
     9
* Initial Contributors:
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    11
*
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    12
* Contributors:
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    13
*
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    14
* Description: 
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    15
*
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    16
*/
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    17
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    18
#include "downloadicon.h"
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    19
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    20
#include <QtNetwork>
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    21
#include <QNetworkRequest>
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    22
#include <QNetworkReply>
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    23
#include <QMessageBox>
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    24
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    25
DownloadIcon::DownloadIcon(Ui::MainWindow &aMainUi)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    26
    :MainUi(aMainUi)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    27
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    28
    manager = new QNetworkAccessManager(this);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    29
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    30
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    31
DownloadIcon::~DownloadIcon()
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    32
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    33
    if(manager)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    34
        {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    35
        manager->disconnect();    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    36
        delete manager;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    37
        manager = 0;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    38
        }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    39
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    40
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    41
void DownloadIcon::ChangePathSlash()
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    42
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    43
    //filePath contains C:\\Data\\ZeroConf\\name.ico
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    44
    //after this method is called it must have
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    45
    //c://data//zeroconf//name.ico
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    46
    int start = 0;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    47
    while( (start = filePath.indexOf("\\",start))!= -1)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    48
        {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    49
            filePath.remove(start,1);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    50
            filePath.insert(start,"//");
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    51
        }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    52
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    53
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    54
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    55
void DownloadIcon::UpdateAvatarItem()
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    56
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    57
    int count = MainUi.listAvatars->count();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    58
    int index = 0;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    59
    while(index < count)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    60
        {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    61
        QListWidgetItem* item = MainUi.listAvatars->item(index);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    62
        if(contactName.compare(item->text()) == 0)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    63
            {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    64
            QPixmap pm;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    65
            //ChangePathSlash();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    66
            //DisplayMsgBox(filePath);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    67
            
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    68
            //if(pm.load(filePath))
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    69
            if(pm.load("E://mypic.ico"))
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    70
                {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    71
                QIcon icon(pm);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    72
                DisplayMsgBox(filePath);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    73
                item->setIcon(icon);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    74
                }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    75
            break;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    76
            }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    77
        index++;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    78
        }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    79
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    80
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    81
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    82
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    83
void DownloadIcon::RequestFinished()
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    84
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    85
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    86
    DisplayMsgBox(tr("Got Response"));
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    87
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    88
    int error = reply->error();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    89
    if(!error)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    90
        {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    91
        QNetworkReply *reply = ((QNetworkReply *)sender());
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    92
        QByteArray data = reply->readAll();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    93
        
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    94
        if(data.isEmpty())
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    95
            DisplayMsgBox(tr("No Data Reply"));
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    96
        
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    97
        //QTextStream out(&data);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    98
        //QString content = out.readAll();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
    99
        
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   100
        //QMessageBox::warning(this, tr("Content"),content,QMessageBox::Ok);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   101
        
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   102
        //QFile temp(filePath);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   103
        QFile temp("E:\\mypic.ico");
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   104
        temp.open(QIODevice::WriteOnly | QIODevice::Truncate);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   105
        temp.write(data);        
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   106
        temp.close();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   107
        UpdateAvatarItem();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   108
        }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   109
    else
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   110
        DisplayMsgBox(reply->errorString());
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   111
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   112
    //Delete this object once the response is received.
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   113
    delete this;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   114
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   115
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   116
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   117
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   118
void DownloadIcon::GetIcon(const TDesC8& aContactName, const TDesC8& aUri, const TDesC8& aFileName)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   119
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   120
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   121
    QString quri = QString::fromUtf8((const char*)aUri.Ptr(),aUri.Length()); 
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   122
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   123
    contactName = QString::fromUtf8((const char*)aContactName.Ptr(),aContactName.Length());
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   124
    filePath = QString::fromUtf8((const char*)aFileName.Ptr(),aFileName.Length());
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   125
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   126
    //DisplayMsgBox(quri);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   127
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   128
#ifdef __arm 
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   129
    reply = manager->get(QNetworkRequest(quri));
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   130
#else
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   131
    reply = manager->get(QNetworkRequest(QUrl("http://kiranp747.googlepages.com/FileList.txt")));
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   132
#endif
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   133
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   134
    
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   135
    connect(reply, SIGNAL(finished()), this, SLOT(RequestFinished()));
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   136
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   137
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   138
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   139
void DownloadIcon::DisplayMsgBox(QString str)
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   140
    {
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   141
    QMessageBox msgBox;
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   142
    msgBox.setText(str);
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   143
    msgBox.exec();
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   144
    }
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   145
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   146
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   147
ff5174af067c ZeroConf Sharing Application
guru.kini@nokia.com
parents:
diff changeset
   148