Skip to content
This repository has been archived by the owner on Dec 9, 2021. It is now read-only.

Qt5 support #81

Closed
wants to merge 12 commits into from
61 changes: 53 additions & 8 deletions CMakeLists.txt
Expand Up @@ -21,7 +21,7 @@ endif()

project(KeePassX)

cmake_minimum_required(VERSION 2.6.4)
cmake_minimum_required(VERSION 2.8.6)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

Expand All @@ -33,6 +33,7 @@ option(WITH_TESTS "Enable building of unit tests" ON)
option(WITH_GUI_TESTS "Enable building of GUI tests" OFF)
option(WITH_LTO "Enable Link Time Optimization (LTO)" OFF)
option(WITH_CXX11 "Build with the C++ 11 standard" ON)
option(WITH_QT5 "Build with Qt5" OFF)

set(KEEPASSX_VERSION "2.0 alpha 6")
set(KEEPASSX_VERSION_NUM "1.9.85")
Expand Down Expand Up @@ -158,16 +159,60 @@ if(WITH_TESTS)
enable_testing()
endif(WITH_TESTS)

set(QT_REQUIRED_MODULES QtCore QtGui QtTest)
set(QT_REQUIRED_MODULES Core Gui Test)
if(UNIX AND NOT APPLE)
set(QT_REQUIRED_MODULES ${QT_REQUIRED_MODULES} QtDBus)
set(QT_REQUIRED_MODULES ${QT_REQUIRED_MODULES} DBus)
endif()

find_package(Qt4 4.6.0 REQUIRED ${QT_REQUIRED_MODULES})
include(${QT_USE_FILE})
# Debian sets the the build type to None for package builds.
# Make sure we don't enable asserts there.
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_NONE QT_NO_DEBUG)
if (WITH_QT5)
set(QT_REQUIRED_MODULES ${QT_REQUIRED_MODULES} Concurrent Widgets LinguistTools)

foreach(QT_REQUIRED_MODULE ${QT_REQUIRED_MODULES})
find_package(Qt5${QT_REQUIRED_MODULE} REQUIRED)

string(TOUPPER ${QT_REQUIRED_MODULE} UPPER_MODULE)
set(QT_QT${UPPER_MODULE}_LIBRARY Qt5${QT_REQUIRED_MODULE})

include_directories(SYSTEM ${Qt5${QT_REQUIRED_MODULE}_INCLUDE_DIRS})
endforeach()
else()
set(OLD_QT_REQUIRED_MODULES ${QT_REQUIRED_MODULES})
set(QT_REQUIRED_MODULES)

foreach(QT_REQUIRED_MODULE ${OLD_QT_REQUIRED_MODULES})
set(QT_REQUIRED_MODULES ${QT_REQUIRED_MODULES} Qt${QT_REQUIRED_MODULE})
endforeach()

find_package(Qt4 4.6.0 REQUIRED ${QT_REQUIRED_MODULES})
include(${QT_USE_FILE})
# Debian sets the the build type to None for package builds.
# Make sure we don't enable asserts there.
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_NONE QT_NO_DEBUG)

foreach (QT_REQUIRED_MODULE ${QT_REQUIRED_MODULES})
string(TOUPPER ${QT_REQUIRED_MODULE} UPPER_MODULE)
include_directories(SYSTEM ${QT_${UPPER_MODULE}_INCLUDE_DIR})
endforeach ()
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
set(CMAKE_AUTOMOC TRUE)

macro(qt_wrap_ui var)
if (WITH_QT5)
qt5_wrap_ui(${var} ${ARGN})
else()
qt4_wrap_ui(${var} ${ARGN})
endif()
endmacro()

macro(qt_add_translation var)
if (WITH_QT5)
qt5_add_translation(${var} ${ARGN})
else()
qt4_add_translation(${var} ${ARGN})
endif()
endmacro()

find_package(Gcrypt REQUIRED)
if(NOT (${GCRYPT_VERSION_STRING} VERSION_LESS "1.6.0"))
Expand Down
2 changes: 1 addition & 1 deletion share/translations/CMakeLists.txt
Expand Up @@ -19,7 +19,7 @@ list(REMOVE_ITEM TRANSLATION_FILES keepassx_en.ts)
list(REMOVE_ITEM TRANSLATION_FILES ${TRANSLATION_EN_ABS})
message(STATUS ${TRANSLATION_FILES})

qt4_add_translation(QM_FILES ${TRANSLATION_FILES})
qt_add_translation(QM_FILES ${TRANSLATION_FILES})

install(FILES ${QM_FILES} DESTINATION ${DATA_INSTALL_DIR}/translations)
add_custom_target(translations DEPENDS ${QM_FILES})
Expand Down
22 changes: 16 additions & 6 deletions src/CMakeLists.txt
Expand Up @@ -43,8 +43,6 @@ set(keepassx_SOURCES
core/ListDeleter.h
core/Metadata.cpp
core/PasswordGenerator.cpp
core/qsavefile.cpp
core/qsavefile_p.h
core/SignalMultiplexer.cpp
core/TimeDelta.cpp
core/TimeInfo.cpp
Expand Down Expand Up @@ -149,7 +147,6 @@ set(keepassx_MOC
core/Group.h
core/InactivityTimer.h
core/Metadata.h
core/qsavefile.h
gui/AboutDialog.h
gui/Application.h
gui/ChangeMasterKeyWidget.h
Expand Down Expand Up @@ -193,6 +190,18 @@ set(keepassx_MOC
streams/SymmetricCipherStream.h
)

if (NOT WITH_QT5)
set(keepassx_SOURCES
${keepassx_SOURCES}
core/qsavefile.cpp
core/qsavefile_p.h
)
set(keepassx_MOC
${keepassx_MOC}
core/qsavefile.h
)
endif ()

set(keepassx_FORMS
gui/AboutDialog.ui
gui/ChangeMasterKeyWidget.ui
Expand Down Expand Up @@ -220,17 +229,18 @@ if(MINGW)
${CMAKE_SOURCE_DIR}/share/windows/icon.rc)
endif()

qt4_wrap_ui(keepassx_SOURCES ${keepassx_FORMS})
qt4_wrap_cpp(keepassx_SOURCES ${keepassx_MOC})
qt_wrap_ui(keepassx_SOURCES ${keepassx_FORMS})

add_library(keepassx_core STATIC ${keepassx_SOURCES})
set_target_properties(keepassx_core PROPERTIES COMPILE_DEFINITIONS KEEPASSX_BUILDING_CORE)
set_target_properties(keepassx_core PROPERTIES
COMPILE_DEFINITIONS KEEPASSX_BUILDING_CORE)

add_executable(${PROGNAME} WIN32 MACOSX_BUNDLE ${keepassx_SOURCES_MAINEXE})
target_link_libraries(${PROGNAME}
keepassx_core
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
${QT_QTWIDGETS_LIBRARY}
${GCRYPT_LIBRARIES}
${ZLIB_LIBRARIES})

Expand Down
4 changes: 3 additions & 1 deletion src/autotype/CMakeLists.txt
Expand Up @@ -5,7 +5,9 @@ if(Q_WS_X11)
endif()

if(X11_FOUND AND X11_XTest_FOUND)
add_subdirectory(x11)
if (WITH_QT5 AND NOT Qt5Core_VERSION_STRING VERSION_LESS 5.1.0)
add_subdirectory(x11)
endif ()
endif()
endif()

Expand Down
2 changes: 2 additions & 0 deletions src/autotype/test/AutoTypeTest.cpp
Expand Up @@ -118,4 +118,6 @@ void AutoTypeExecturorTest::execKey(AutoTypeKey* action)
m_platform->addActionKey(action);
}

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
Q_EXPORT_PLUGIN2(keepassx-autotype-test, AutoTypePlatformTest)
#endif
3 changes: 3 additions & 0 deletions src/autotype/test/AutoTypeTest.h
Expand Up @@ -31,6 +31,9 @@ class AutoTypePlatformTest : public QObject,
{
Q_OBJECT
Q_INTERFACES(AutoTypePlatformInterface AutoTypeTestInterface)
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
Q_PLUGIN_METADATA(IID "org.keepassx.AutoTypePlatformInterface/1" FILE "autotypetest.json")
#endif

public:
QString keyToString(Qt::Key key);
Expand Down
4 changes: 1 addition & 3 deletions src/autotype/test/CMakeLists.txt
Expand Up @@ -6,7 +6,5 @@ set(autotype_test_MOC
AutoTypeTest.h
)

qt4_wrap_cpp(autotype_test_SOURCES ${autotype_test_MOC})

add_library(keepassx-autotype-test MODULE ${autotype_test_SOURCES})
target_link_libraries(keepassx-autotype-test testautotype ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
target_link_libraries(keepassx-autotype-test testautotype ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTWIDGETS_LIBRARY})
4 changes: 4 additions & 0 deletions src/autotype/x11/AutoTypeX11.cpp
Expand Up @@ -19,6 +19,8 @@
#include "AutoTypeX11.h"
#include "KeySymMap.h"

#include <X11/Intrinsic.h>

#include <time.h>

bool AutoTypePlatformX11::m_catchXErrors = false;
Expand Down Expand Up @@ -721,4 +723,6 @@ int AutoTypePlatformX11::initialTimeout()
return 500;
}

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
Q_EXPORT_PLUGIN2(keepassx-autotype-x11, AutoTypePlatformX11)
#endif
3 changes: 3 additions & 0 deletions src/autotype/x11/AutoTypeX11.h
Expand Up @@ -39,6 +39,9 @@ class AutoTypePlatformX11 : public QObject, public AutoTypePlatformInterface
{
Q_OBJECT
Q_INTERFACES(AutoTypePlatformInterface)
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
Q_PLUGIN_METADATA(IID "org.keepassx.AutoTypePlatformInterface/1" FILE "autotypex11.json")
#endif

public:
AutoTypePlatformX11();
Expand Down
11 changes: 8 additions & 3 deletions src/autotype/x11/CMakeLists.txt
@@ -1,5 +1,12 @@
include_directories(SYSTEM ${X11_X11_INCLUDE_PATH})

set(qt5_libs)
if (WITH_QT5)
find_package(Qt5X11Extras REQUIRED)
list(APPEND qt5_libs Qt5X11Extras)
include_directories(SYSTEM ${Qt5X11Extras_INCLUDE_DIRS})
endif ()

set(autotype_X11_SOURCES
AutoTypeX11.cpp
)
Expand All @@ -8,10 +15,8 @@ set(autotype_X11_MOC
AutoTypeX11.h
)

qt4_wrap_cpp(autotype_X11_SOURCES ${autotype_X11_MOC})

add_library(keepassx-autotype-x11 MODULE ${autotype_X11_SOURCES})
target_link_libraries(keepassx-autotype-x11 ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${X11_X11_LIB} ${X11_XTest_LIB})
target_link_libraries(keepassx-autotype-x11 ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTWIDGETS_LIBRARY} ${qt5_libs} ${X11_X11_LIB} ${X11_XTest_LIB})
install(TARGETS keepassx-autotype-x11
BUNDLE DESTINATION . COMPONENT Runtime
LIBRARY DESTINATION ${PLUGIN_INSTALL_DIR} COMPONENT Runtime)
1 change: 1 addition & 0 deletions src/core/Database.h
Expand Up @@ -20,6 +20,7 @@

#include <QDateTime>
#include <QHash>
#include <QObject>

#include "core/Uuid.h"
#include "keys/CompositeKey.h"
Expand Down
8 changes: 8 additions & 0 deletions src/core/qsavefile.cpp
Expand Up @@ -42,7 +42,9 @@
#include "qsavefile.h"
#include "qsavefile_p.h"

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QAbstractFileEngine>
#endif
#include <QFileInfo>
#include <QTemporaryFile>

Expand Down Expand Up @@ -297,11 +299,17 @@ bool QSaveFile::commit()
QFile::remove(bakname);
QFile::rename(d->fileName, bakname);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
if (!d->tempFile->rename(d->fileName)) {
d->error = d->tempFile->error();
setErrorString(d->tempFile->errorString());
#else
QAbstractFileEngine* fileEngine = d->tempFile->fileEngine();
Q_ASSERT(fileEngine);
if (!fileEngine->rename(d->fileName)) {
d->error = fileEngine->error();
setErrorString(fileEngine->errorString());
#endif
d->tempFile->remove();
delete d->tempFile;
d->tempFile = 0;
Expand Down
10 changes: 9 additions & 1 deletion src/gui/entry/EditEntryWidget.cpp
Expand Up @@ -22,9 +22,9 @@
#include "ui_EditEntryWidgetMain.h"

#include <QDesktopServices>
#include <QStackedLayout>
#include <QMenu>
#include <QSortFilterProxyModel>
#include <QStackedLayout>
#include <QTemporaryFile>

#include "core/Config.h"
Expand Down Expand Up @@ -594,7 +594,11 @@ void EditEntryWidget::insertAttachment()

QString defaultDir = config()->get("LastAttachmentDir").toString();
if (defaultDir.isEmpty() || !QDir(defaultDir).exists()) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
defaultDir = (QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation) << "").first();
#else
defaultDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
#endif
}
QString filename = fileDialog()->getOpenFileName(this, tr("Select file"), defaultDir);
if (filename.isEmpty() || !QFile::exists(filename)) {
Expand Down Expand Up @@ -628,7 +632,11 @@ void EditEntryWidget::saveCurrentAttachment()
QString filename = m_attachmentsModel->keyByIndex(index);
QString defaultDirName = config()->get("LastAttachmentDir").toString();
if (defaultDirName.isEmpty() || !QDir(defaultDirName).exists()) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
defaultDirName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
defaultDirName = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
#endif
}
QDir dir(defaultDirName);
QString savePath = fileDialog()->getSaveFileName(this, tr("Save attachment"),
Expand Down
9 changes: 9 additions & 0 deletions src/gui/entry/EntryModel.cpp
Expand Up @@ -29,7 +29,9 @@ EntryModel::EntryModel(QObject* parent)
: QAbstractTableModel(parent)
, m_group(Q_NULLPTR)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
setSupportedDragActions(Qt::MoveAction | Qt::CopyAction);
#endif
}

Entry* EntryModel::entryFromIndex(const QModelIndex& index) const
Expand Down Expand Up @@ -99,6 +101,13 @@ void EntryModel::setEntryList(const QList<Entry*>& entries)
Q_EMIT switchedToEntryListMode();
}

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
Qt::DropActions EntryModel::supportedDragActions() const
{
return (Qt::MoveAction | Qt::CopyAction);
}
#endif

int EntryModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
Expand Down
4 changes: 4 additions & 0 deletions src/gui/entry/EntryModel.h
Expand Up @@ -53,6 +53,10 @@ class EntryModel : public QAbstractTableModel

void setEntryList(const QList<Entry*>& entries);

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
virtual Qt::DropActions supportedDragActions() const;
#endif

Q_SIGNALS:
void switchedToEntryListMode();
void switchedToGroupMode();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/entry/EntryView.cpp
Expand Up @@ -32,7 +32,9 @@ EntryView::EntryView(QWidget* parent)
m_sortModel->setDynamicSortFilter(true);
m_sortModel->setSortLocaleAware(true);
m_sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
m_sortModel->setSupportedDragActions(m_model->supportedDragActions());
#endif
QTreeView::setModel(m_sortModel);

setUniformRowHeights(true);
Expand Down
1 change: 1 addition & 0 deletions src/gui/group/GroupView.cpp
Expand Up @@ -19,6 +19,7 @@

#include <QDragMoveEvent>
#include <QMetaObject>
#include <QMimeData>

#include "core/Database.h"
#include "core/Group.h"
Expand Down