Clean up evaluation license logic

Instead of storing the application type as a uint, we use the enum provided
by QCoreApplicationPrivate. The former resulted in a few cases of wrong
logic where the values got mixed up, such as always printing the QtCore
console warning, even for GUI applications.

The qt_eval_is_supported function has been refactored to return enums instead
of magic values, to make the logic easier to read.

The same goes for qt_eval_days_left, which now only concerns itself with
the number of days left. qt_eval_is_expired() has been added to use for
easy checking of expiration date.

Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Change-Id: Ia0e85b2103f790a7e02e0d6e567a477b3145fcb9
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@digia.com>
This commit is contained in:
Tor Arne Vestbø 2013-07-04 15:44:53 +02:00 committed by The Qt Project
parent 1bd8db01c4
commit 563c9445d4
5 changed files with 48 additions and 51 deletions

View File

@ -479,6 +479,8 @@ QT_END_NAMESPACE
# include <stdio.h>
# include <stdlib.h>
#include "private/qcoreapplication_p.h"
extern const char qt_core_interpreter[] __attribute__((section(".interp")))
= ELF_INTERPRETER;
@ -501,8 +503,8 @@ void qt_core_boilerplate()
QT_PREPEND_NAMESPACE(qDumpCPUFeatures)();
#ifdef QT_EVAL
extern void qt_core_eval_init(uint);
qt_core_eval_init(1);
extern void qt_core_eval_init(QCoreApplicationPrivate::Type);
qt_core_eval_init(QCoreApplicationPrivate::Tty);
#endif
exit(0);

View File

@ -381,7 +381,7 @@ QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv, uint
, origArgc(aargc)
, origArgv(new char *[aargc])
#endif
, application_type(0)
, application_type(QCoreApplicationPrivate::Tty)
#ifndef QT_NO_QOBJECT
, in_exec(false)
, aboutToQuitEmitted(false)
@ -724,7 +724,7 @@ void QCoreApplication::init()
#endif
#ifdef QT_EVAL
extern void qt_core_eval_init(uint);
extern void qt_core_eval_init(QCoreApplicationPrivate::Type);
qt_core_eval_init(d->application_type);
#endif

View File

@ -135,7 +135,7 @@ public:
static bool isTranslatorInstalled(QTranslator *translator);
#endif
uint application_type;
QCoreApplicationPrivate::Type application_type;
QString cachedApplicationDirPath;
QString cachedApplicationFilePath;

View File

@ -44,6 +44,7 @@
#include <qlibraryinfo.h>
#include <qobject.h>
#include <qcoreapplication.h>
#include <private/qcoreapplication_p.h>
#include "stdio.h"
#include "stdlib.h"
@ -52,7 +53,7 @@ QT_BEGIN_NAMESPACE
#include "qconfig_eval.cpp"
static const char boilerplate_unsuported[] =
static const char boilerplate_supported_but_time_limited[] =
"\nQt %1 Evaluation License\n"
"Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).\n"
"This trial version may only be used for evaluation purposes\n"
@ -86,13 +87,19 @@ static const char will_shutdown_now[] =
"timeout and will shut down.\n"
"Contact http://qt.digia.com/contact-us for pricing and purchasing information.\n";
static int qt_eval_is_supported()
enum EvaluationStatus {
EvaluationNotSupported = 0,
EvaluationSupportedButTimeLimited,
EvaluationSupported
};
static EvaluationStatus qt_eval_is_supported()
{
const volatile char *const license_key = qt_eval_key_data + 12;
// fast fail
if (!qt_eval_key_data[0] || !*license_key)
return -1;
return EvaluationNotSupported;
// is this an unsupported evaluation?
const volatile char *typecode = license_key;
@ -103,31 +110,33 @@ static int qt_eval_is_supported()
if (!field && typecode[1] == '4' && typecode[2] == 'M') {
if (typecode[0] == 'Q')
return 0;
return EvaluationSupportedButTimeLimited;
else if (typecode[0] == 'R' || typecode[0] == 'Z')
return 1;
return EvaluationSupported;
}
return -1;
return EvaluationNotSupported;
}
static int qt_eval_days_left()
{
if (qt_eval_is_supported() < 0)
return -2;
QDate today = QDate::currentDate();
QDate build = QLibraryInfo::buildDate();
return qMax<qint64>(-1, today.daysTo(build) + 30);
}
static bool qt_eval_is_expired()
{
return qt_eval_days_left() < 0;
}
static QString qt_eval_string()
{
const char *msg;
switch (qt_eval_is_supported()) {
case 0:
msg = boilerplate_unsuported;
case EvaluationSupportedButTimeLimited:
msg = boilerplate_supported_but_time_limited;
break;
case 1:
case EvaluationSupported:
msg = boilerplate_supported;
break;
default:
@ -153,7 +162,7 @@ public:
QCoreFuriCuri() : QObject(), warn(-1), kill(-1)
{
if (!qt_eval_is_supported()) {
if (qt_eval_is_supported() == EvaluationSupportedButTimeLimited) {
warn = startTimer(WARN_TIMEOUT);
kill = 0;
}
@ -173,27 +182,20 @@ public:
#if defined(QT_BUILD_CORE_LIB) || defined (QT_BOOTSTRAPPED)
void qt_core_eval_init(uint type)
void qt_core_eval_init(QCoreApplicationPrivate::Type type)
{
if (!type)
return; // GUI app
switch (qt_eval_days_left()) {
case -2:
if (type != QCoreApplicationPrivate::Tty)
return;
case -1:
fprintf(stderr, "%s\n", boilerplate_expired);
if (type == 0) {
// if we're a console app only.
exit(0);
}
if (!qt_eval_is_supported())
return;
default:
if (qt_eval_is_expired()) {
fprintf(stderr, "%s\n", boilerplate_expired);
exit(0);
} else {
fprintf(stderr, "%s\n", qPrintable(qt_eval_string()));
if (type == 0) {
Q_UNUSED(new QCoreFuriCuri());
}
Q_UNUSED(new QCoreFuriCuri());
}
}
#endif
@ -453,12 +455,7 @@ public:
{
setWindowTitle(QLatin1String(" "));
QString str = qt_eval_string();
if (expired) {
str = QLatin1String(boilerplate_expired);
} else {
str = qt_eval_string();
}
QString str = expired ? QLatin1String(boilerplate_expired) : qt_eval_string();
str = str.trimmed();
QFrame *border = new QFrame(this);
@ -520,24 +517,22 @@ public:
};
void qt_gui_eval_init(uint)
void qt_gui_eval_init(QCoreApplicationPrivate::Type type)
{
switch (qt_eval_days_left()) {
case -2:
Q_UNUSED(type);
if (!qt_eval_is_supported())
return;
case -1: {
if (qt_eval_is_expired()) {
EvalMessageBox box(true);
box.exec();
::exit(0);
}
default: {
} else {
EvalMessageBox *box = new EvalMessageBox(false);
box->show();
Q_UNUSED(new QGuiFuriCuri());
}
}
}
static QString qt_eval_title_prefix()
@ -547,14 +542,14 @@ static QString qt_eval_title_prefix()
QString qt_eval_adapt_window_title(const QString &title)
{
if (qt_eval_days_left() == -2)
if (!qt_eval_is_supported())
return title;
return qt_eval_title_prefix() + title;
}
void qt_eval_init_widget(QWidget *w)
{
if (qt_eval_days_left() == -2)
if (!qt_eval_is_supported())
return;
if (w->isTopLevel() && w->windowTitle().isEmpty() && w->windowType() != Qt::Desktop ) {
w->setWindowTitle(QLatin1String(" "));

View File

@ -556,7 +556,7 @@ void QApplicationPrivate::construct()
eventDispatcher->startingUp();
#ifdef QT_EVAL
extern void qt_gui_eval_init(uint);
extern void qt_gui_eval_init(QCoreApplicationPrivate::Type);
qt_gui_eval_init(application_type);
#endif