summaryrefslogtreecommitdiffstats
path: root/util/install/mac/unpackdlgimpl.cpp
blob: 5538f4db05697e9ea50f70ca57cd2b2fae7b3390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the utils of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "unpackdlgimpl.h"
#include "licensedlgimpl.h"
#include "keyinfo.h"
#include <qdir.h>
#include <qfiledialog.h>
#include <qlineedit.h>
#include <qtextview.h>
#include <qpushbutton.h>
#include <qarchive.h>
#include <qmessagebox.h>
#include <qregexp.h>
#include <qapplication.h>
#include <unistd.h>
#include <stdlib.h>

UnpackDlgImpl::UnpackDlgImpl( QString key, QWidget* pParent, const char* pName, WindowFlags f ) : 
    UnpackDlg( pParent, pName, f )
{
    destPath->setText( QDir::currentDirPath() );
    if(!key.isNull()) {
	srcKey->setText(key);
    } else if(QFile::exists( QDir::homeDirPath() + "/.qt-license")) {
	QFile lic( QDir::homeDirPath() + "/.qt-license");
	if( lic.open( IO_ReadOnly ) ) {
	    QString buffer;
	    while( lic.readLine( buffer, 1024 ) != -1 ) {
		if( buffer[ 0 ] != '#' ) {
		    QStringList components = QStringList::split( '=', buffer );
		    QStringList::Iterator it = components.begin();
		    QString key = (*it++).stripWhiteSpace().replace( QRegExp( QString( "\"" ) ), QString() );
		    if(key.upper() == "LICENSEKEY") {
			QString value = (*it++).stripWhiteSpace().replace( QRegExp( QString( "\"" ) ), QString() );
			srcKey->setText(value);
		    }
		}
	    }
	    lic.close();
	}
    }
    connect( srcKey, SIGNAL( textChanged( const QString& ) ), this, SLOT( licenseKeyChanged() ) );
    licenseKeyChanged();
    logOutput->setWordWrap( QTextView::WidgetWidth );
    logOutput->setWrapPolicy( QTextView::Anywhere );
}

void UnpackDlgImpl::clickedDestButton()
{
    QString dest =  QFileDialog::getExistingDirectory( destPath->text(), this, NULL, "Select destination directory" );
    if (!dest.isNull())
	destPath->setText( dest );
}

void UnpackDlgImpl::clickedUnpack()
{
    QArchive archive;
    connect( &archive, SIGNAL( operationFeedback( const QString& ) ), this, SLOT( updateProgress( const QString& ) ) );
    connect( &archive, SIGNAL( operationFeedback( int ) ), this, SLOT( updateProgress( int ) ) );

    archive.setVerbosity( QArchive::Destination | QArchive::Verbose | QArchive::Progress );

    QString dest = destPath->text(), src="qt-mac-commercial-3.0.0.app/Contents/Qt/qtmac.arq";
    if(!dest.isEmpty() && dest.right(1) != "/")
	dest += "/";
    archive.setPath( src );
    if( !archive.open( IO_ReadOnly ) ) {
	QMessageBox::critical( NULL, "Failure", "Failed to open input " + src);
	return;
    } else if(!QFile::exists(dest) ) {
	QDir d;
	if(!d.mkdir(dest)) {
	    QMessageBox::critical( NULL, "Failure", "Failed to open directory " + dest);
	    return;
	}
    }
    unpackButton->setDisabled( true );
    srcKey->setDisabled( true );
    destPath->setDisabled( true );
    destButton->setDisabled( true );
    LicenseDialogImpl licenseDialog( this );
    if((!licenseDialog.showLicense( featuresForKeyOnUnix( srcKey->text() ) & Feature_US )) ||
       (!licenseDialog.exec())) {
	QMessageBox::critical( NULL, "Failure", "The license agreement was rejected." );
	updateProgress( "License rejected" );
	unpackButton->setDisabled( false );
	srcKey->setDisabled( false );
	destPath->setDisabled( false );
	destButton->setDisabled( false );
	return;
    }

    QString srcName  = "qt-mac-commercial-3.0.0.app/Contents/Qt/LICENSE";
    QString destName = "/qt-mac-commercial-3.0.0/.LICENSE";
    QString srcName2 = srcName;
    if ( featuresForKeyOnUnix( srcKey->text() ) & Feature_US )
	srcName2 = srcName2 + "-US";
    if((!archive.readArchive( dest, srcKey->text() )) ||
       (!copyFile( srcName, dest + destName )) ||
       (!copyFile( srcName + "-US", dest + destName + "-US" )) ||
       (!copyFile( srcName2, dest + "/qt-mac-commercial-3.0.0/LICENSE" ))) 
    {
	QMessageBox::critical( NULL, "Failure", "Failed to unpack " + src);
	archive.close();
    }	
    else {
	QMessageBox::information( NULL, "Archive unpacked", "Qt has been "
	   "extracted to " + dest + "qt-mac-commerical-3.0.0\nPlease read "
	   "the INSTALL.macosx file for installation instructions."  );
	cancelButton->setText( "Quit" );
    }
}

bool UnpackDlgImpl::copyFile( const QString& src, const QString& dest )
{
    int len;
    const int buflen = 4096;
    char buf[buflen];
    QFileInfo info( src );
    QFile srcFile( src ), destFile( dest );
    if (!srcFile.open( IO_ReadOnly ))
	return false;
    destFile.remove();
    if (!destFile.open( IO_WriteOnly )) {
	srcFile.close();
	return false;
    }
    while (!srcFile.atEnd()) {
	len = srcFile.readBlock( buf, buflen );
	if (len <= 0)
	    break;
	if (destFile.writeBlock( buf, len ) != len)
	    return false;
    }
    destFile.flush();
    return true;
}

void UnpackDlgImpl::updateProgress( const QString& message )
{
    logOutput->append( message );
}

void UnpackDlgImpl::updateProgress( int )
{
    qApp->processEvents();
}

void UnpackDlgImpl::licenseKeyChanged()
{
    QRegExp keyExpr("^....-....-....");
    if ((keyExpr.search( srcKey->text() ) != -1) && 
	(featuresForKey( srcKey->text() ) & Feature_Mac))
	unpackButton->setEnabled( true );
    else
	unpackButton->setEnabled( false );
}

void UnpackDlgImpl::reject()
{
    exit( 0 );
}