Commit 03262b5f authored by Andrius Štikonas's avatar Andrius Štikonas

Remove KDELibs4Support from KSudoku

Summary:
Port KNetAccess to KIO.
Change signature of Serializer::load, Serializer::loadCustomShape to use QString& instead of QString*
Change signature of Serializer::store: pass errorMsg
Remove unused function.
Port remaining Qt4 calls such as toAscii() or setAcceptsHoverEvents(),

Test Plan: Game can be played, tested loading and saving of current game.

Reviewers: #kde_games, ltoscano, ouwerkerk

Reviewed By: ltoscano, ouwerkerk

Differential Revision: https://phabricator.kde.org/D5627
parent 842a1e20
......@@ -7,18 +7,20 @@ set (KF5_MIN_VERSION "5.31.0")
find_package(ECM ${KF5_MIN_VERSION} REQUIRED CONFIG)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR})
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS Widgets Qml Quick QuickWidgets Svg Test OpenGL)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS Widgets Qml Quick QuickWidgets PrintSupport Svg Test OpenGL)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
Archive
Config
ConfigWidgets
CoreAddons
Crash
DBusAddons
DocTools
GuiAddons
I18n
ItemModels
KDELibs4Support
KIO
JobWidgets
NewStuff
NotifyConfig
TextWidgets
......
......@@ -36,11 +36,14 @@ add_library(ksudoku_gui STATIC ${ksudoku_SRCS} ${ksudoku_views_SRCS})
target_link_libraries(ksudoku_gui
ksudoku_logic
KF5KDEGames
KF5::Archive
KF5::KDELibs4Support
KF5::Archive
KF5::GuiAddons
KF5::KIOCore
KF5::KIOWidgets
KF5::XmlGui
Qt5::Core
Qt5::Gui
Qt5::PrintSupport
Qt5::Svg
)
......
......@@ -24,11 +24,12 @@
#include "serializer.h"
#include <QDebug>
#include <KMessageBox>
#include <KLocalizedString>
#include <QPainter>
#include <QIcon>
#include <QEvent>
#include <QIcon>
#include <QPainter>
#include <KLocalizedString>
#include <KMessageBox>
......@@ -416,7 +417,8 @@ bool CustomGame::createSKGraphObject()
m_graph = 0; // Killer Sudoku game (re-inits cages and size).
}
if (m_graph == 0) {
m_graph = ksudoku::Serializer::loadCustomShape(m_url, 0, 0);
QString errorMsg;
m_graph = ksudoku::Serializer::loadCustomShape(m_url, 0, errorMsg);
}
return (m_graph != 0); // True if the shapes/*.xml file loaded OK.
}
......
......@@ -2,8 +2,7 @@
* Copyright 2005-2007 Francesco Rossi <redsh@email.it> *
* Copyright 2006-2007 Mick Kappenburg <ksudoku@kappendburg.net> *
* Copyright 2006-2008 Johannes Bergmeier <johannes.bergmeier@gmx.net> *
* Copyright 2012 Ian Wadham <iandw.au@gmail.com> *
* Copyright 2015 Ian Wadham <iandw.au@gmail.com> *
* Copyright 2012,2015 Ian Wadham <iandw.au@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -25,35 +24,39 @@
#include "globals.h"
#include "ksudoku.h"
#include <QAction>
#include <QComboBox>
#include <QDir>
#include <QDebug>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QFileDialog>
#include <QFileInfo>
#include <QMimeData>
#include <QLabel>
#include <QHBoxLayout>
#include <QKeySequence>
#include <QPrinter>
#include <QPrintDialog>
#include <QStandardPaths>
#include <QStatusBar>
#include <QTemporaryFile>
#include <QUrl>
#include <KLocalizedString>
#include <KActionCollection>
#include <KStandardAction>
#include <QAction>
#include <KConfigDialog>
#include <KIO/Job>
#include <KJobWidgets>
#include <KLocalizedString>
#include <KMessageBox>
#include <KRun>
#include <KSharedConfig>
#include <KStandardAction>
#include <KStandardGameAction>
#include <KTar>
#define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
#include <libkdegamesprivate/kgamethemeselector.h>
#include <QKeySequence>
#include <QUrl>
#include <kmessagebox.h>
#include <KLocalizedString>
#include <qstatusbar.h>
#include <kio/netaccess.h>
#include <krun.h>
#include "ksview.h"
#include "gameactions.h"
......@@ -65,14 +68,6 @@
#include "puzzleprinter.h"
#include <ktar.h>
#include <kio/job.h>
#include <kstandardgameaction.h>
#include <QDir>
#include <KSharedConfig>
#include <QFileDialog>
#include <QFileInfo>
#include "gamevariants.h"
#include "welcomescreen.h"
#include "valuelistwidget.h"
......@@ -373,7 +368,7 @@ void KSudoku::endCurrentGame() {
void KSudoku::loadGame(const QUrl& Url) {
QString errorMsg;
Game game = ksudoku::Serializer::load(Url, this, &errorMsg);
const Game game = ksudoku::Serializer::load(Url, this, errorMsg);
if(!game.isValid()) {
KMessageBox::information(this, errorMsg);
return;
......@@ -648,9 +643,12 @@ void KSudoku::dropEvent(QDropEvent *event)
// okay, we have a URI.. process it
const QUrl &Url = Urls.first();
Game game = ksudoku::Serializer::load(Url, this);
QString errorMsg;
const Game game = ksudoku::Serializer::load(Url, this, errorMsg);
if(game.isValid())
startGame(game);
else
KMessageBox::error(this, errorMsg, i18n("Could not load game."));
}
}
}
......@@ -686,9 +684,10 @@ void KSudoku::gameOpen()
if (!Url.isEmpty() && Url.isValid())
{
Game game = ksudoku::Serializer::load(Url, this);
QString errorMsg;
Game game = ksudoku::Serializer::load(Url, this, errorMsg);
if(!game.isValid()) {
KMessageBox::error(this, i18n("Could not load game."));
KMessageBox::error(this, errorMsg, i18n("Could not load game."));
return;
}
......@@ -711,8 +710,12 @@ void KSudoku::gameSave()
if(!game.isValid()) return;
if(game.getUrl().isEmpty()) game.setUrl(QFileDialog::getSaveFileUrl());
if (!game.getUrl().isEmpty() && game.getUrl().isValid())
ksudoku::Serializer::store(game, game.getUrl(), this);
if (!game.getUrl().isEmpty() && game.getUrl().isValid())
{
QString errorMsg;
if (!ksudoku::Serializer::store(game, game.getUrl(), this, errorMsg))
KMessageBox::error(this, errorMsg, i18n("Error Writing File"));
}
}
void KSudoku::gameSaveAs()
......@@ -862,43 +865,6 @@ ksudoku::KsView* KSudoku::currentView() const{
return m_gameUI;
}
void KSudoku::loadCustomShapeFromPath()
{
QUrl Url = QFileDialog::getOpenFileUrl(this, i18n("Open Location"), QUrl(), QString());
if ( Url.isEmpty() || !Url.isValid() )
{
return; // user cancelled
}
QString tmpFile;
if(!KIO::NetAccess::download( Url, tmpFile, this ))
{
//TODO ERROR
return;
}
const QString destDir = QStandardPaths::writableLocation( QStandardPaths::GenericDataLocation ) + QStringLiteral("/ksudoku/");
QDir().mkpath( destDir );
KTar archive( tmpFile );
if ( archive.open( QIODevice::ReadOnly ) )
{
const KArchiveDirectory *archiveDir = archive.directory();
archiveDir->copyTo( destDir );
archive.close();
}
else
{
//just copy
KIO::file_copy (Url, QUrl::fromLocalFile(destDir + '/' + Url.fileName()));
}
KIO::NetAccess::removeTempFile(tmpFile);
updateShapesList();
}
void KSudoku::enableMessages()
{
// Enable all messages that the user has marked "Do not show again".
......
......@@ -91,7 +91,6 @@ public:
public:
void updateShapesList();
void loadCustomShapeFromPath();
void createCustomShape();
ksudoku::Game currentGame() const;
......
......@@ -24,16 +24,17 @@
#include "ksudokugame.h"
#include "puzzle.h"
#include <qdom.h>
//Added by qt3to4:
#include <QDomDocument>
#include <QFile>
#include <QList>
#include <QTextStream>
#include <QUrl>
#include <QTemporaryFile>
#include <kio/netaccess.h>
#include <qfile.h>
#include <KLocalizedString>
#include <KIO/FileCopyJob>
#include <KJobWidgets>
#include <KLocalizedString>
#include <KMessageBox>
#include "ksudoku.h"
#include "symbols.h"
......@@ -398,29 +399,32 @@ HistoryEvent Serializer::deserializeComplexHistoryEvent(QDomElement /*element*/)
return HistoryEvent();
}
SKGraph *Serializer::loadCustomShape(const QUrl &url, QWidget* window, QString *errorMsg) {
if ( url.isEmpty() ) return 0;
QString tmpFile;
bool success = false;
SKGraph *Serializer::loadCustomShape(const QUrl& url, QWidget* window, QString& errorMsg) {
if ( url.isEmpty() ) {
errorMsg = i18n("Unable to download file: URL is empty.");
return nullptr;
}
QDomDocument doc;
if(KIO::NetAccess::download(url, tmpFile, window) ) {
QFile file(tmpFile);
if(file.open(QIODevice::ReadOnly)) {
int errorLine;
if(!doc.setContent(&file, 0, &errorLine)) {
if(errorMsg)
*errorMsg = i18n("Cannot read XML file on line %1", errorLine);
return 0;
}
success = true;
}
KIO::NetAccess::removeTempFile(tmpFile);
QTemporaryFile tmpFile;
if ( !tmpFile.open() ) {
errorMsg = i18n("Unable to create temporary file.");
return nullptr;
}
if ( !success ) {
if(errorMsg)
*errorMsg = i18n("Cannot load file.");
return 0;
KIO::FileCopyJob *downloadJob = KIO::file_copy(url, QUrl::fromLocalFile(tmpFile.fileName()), -1, KIO::Overwrite);
KJobWidgets::setWindow(downloadJob, window);
downloadJob->exec();
if( downloadJob->error() ) {
errorMsg = i18n("Unable to download file.");
return nullptr;
}
int errorLine;
if(!doc.setContent(&tmpFile, 0, &errorLine)) {
errorMsg = i18n("Cannot read XML file on line %1", errorLine);
return nullptr;
}
QDomNode child = doc.documentElement().firstChild();
......@@ -433,30 +437,30 @@ SKGraph *Serializer::loadCustomShape(const QUrl &url, QWidget* window, QString *
child = child.nextSibling();
}
return 0;
return nullptr;
}
Game Serializer::load(const QUrl& url, QWidget* window, QString *errorMsg) {
Game Serializer::load(const QUrl& url, QWidget* window, QString& errorMsg) {
if ( url.isEmpty() ) return Game();
QString tmpFile;
bool success = false;
QDomDocument doc;
if(KIO::NetAccess::download(url, tmpFile, window) ) {
QFile file(tmpFile);
if(file.open(QIODevice::ReadOnly)) {
int errorLine;
if(!doc.setContent(&file, 0, &errorLine)) {
if(errorMsg)
*errorMsg = i18n("Cannot read XML file on line %1", errorLine);
return Game();
}
success = true;
}
KIO::NetAccess::removeTempFile(tmpFile);
QTemporaryFile tmpFile;
if ( !tmpFile.open() ) {
errorMsg = i18n("Unable to create temporary file.");
return Game();
}
if ( !success ) {
if(errorMsg)
*errorMsg = i18n("Cannot load file.");
KIO::FileCopyJob *downloadJob = KIO::file_copy(url, QUrl::fromLocalFile(tmpFile.fileName()), -1, KIO::Overwrite);
KJobWidgets::setWindow(downloadJob, window);
downloadJob->exec();
if( downloadJob->error() ) {
errorMsg = i18n("Unable to download file.");
return Game();
}
int errorLine;
if(!doc.setContent(&tmpFile, 0, &errorLine)) {
errorMsg = i18n("Cannot read XML file on line %1", errorLine);
return Game();
}
......@@ -682,7 +686,7 @@ bool Serializer::serializeHistoryEvent(QDomElement& parent, const HistoryEvent&
return true;
}
bool Serializer::store(const Game& game, const QUrl& url, QWidget* window) {
bool Serializer::store(const Game& game, const QUrl& url, QWidget* window, QString& errorMsg) {
QDomDocument doc( "ksudoku" );
QDomElement root = doc.createElement( "ksudoku" );
doc.appendChild( root );
......@@ -690,13 +694,23 @@ bool Serializer::store(const Game& game, const QUrl& url, QWidget* window) {
serializeGame(root, game);
QTemporaryFile file;
file.open();
if ( !file.open() ) {
errorMsg = i18n("Unable to create temporary file.");
return false;
}
QTextStream stream(&file);
stream << doc.toString();
stream.flush();
KIO::NetAccess::upload(file.fileName(), url, window);
KIO::FileCopyJob *copyJob = KIO::file_copy(QUrl::fromLocalFile(file.fileName()), url);
KJobWidgets::setWindow(copyJob , window);
copyJob->exec();
if(copyJob->error())
{
errorMsg = i18n("Unable to upload file.");
return false;
}
return true;
}
......
......@@ -39,11 +39,11 @@ class HistoryEvent;
class Serializer {
public:
static SKGraph* loadCustomShape
(const QUrl& url, QWidget* window, QString* errorMsg = 0);
(const QUrl& url, QWidget* window, QString& errorMsg);
static bool store
(const Game& game, const QUrl& url, QWidget* window);
(const Game& game, const QUrl& url, QWidget* window, QString& errorMsg);
static Game load
(const QUrl& url, QWidget* window, QString* errorMsg = 0);
(const QUrl& url, QWidget* window, QString& errorMsg);
private:
// TODO - IDW. Maybe there should be shared methods for file handling.
......
......@@ -26,7 +26,7 @@
namespace ksudoku {
/// returns the symbol vor a value used for loading and saving
/// returns the symbol for a value used for loading and saving
QChar Symbols::ioValue2Symbol(int value) {
if (value == VACANT) return '_';
if (value == UNUSABLE) return '.';
......@@ -35,7 +35,7 @@ QChar Symbols::ioValue2Symbol(int value) {
/// returns the number of the index
int Symbols::ioSymbol2Value(const QChar& symbol) {
char c = symbol.toAscii();
char c = symbol.toLatin1();
if(symbol == '_') return VACANT;
if(symbol == '.') return UNUSABLE;
return c - 'a';
......
......@@ -71,7 +71,7 @@ private:
};
CellGraphicsItem::CellGraphicsItem(QPoint pos, int id, View2DScene* scene) {
setAcceptsHoverEvents(true);
setAcceptHoverEvents(true);
setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
m_pos = pos;
m_size = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment