Skip to content

Commit

Permalink
Database write error message
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-melnik committed Apr 10, 2016
1 parent 191de4a commit a113b09
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/filmslist/filmslistloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool FilmsListLoader::Populate( FilmItem* rootItem, QString fileName )
}


bool FilmsListLoader::Save( FilmItem* rootItem, QString fileName )
bool FilmsListLoader::Save( FilmItem* rootItem, QString fileName, QString* errorString )
{
DebugPrintFunc( "FilmsListLoader::Save", fileName );

Expand All @@ -93,18 +93,25 @@ bool FilmsListLoader::Save( FilmItem* rootItem, QString fileName )

QFile file( fileName );

if( file.open( QIODevice::WriteOnly ) || file.write(data) == -1 )
if( file.open( QIODevice::WriteOnly ) )
{
QDataStream stream( &file );
stream.setVersion( QDataStream::Qt_5_3 );
stream << Alexandra::databaseHeader;
stream << Alexandra::databaseVersion;
stream << data;
file.close();
}
else
{
DebugPrint( "Saving failed!" );
DebugPrintFuncDone( "FilmsListLoader::Save" );

if (errorString != nullptr)
{
*errorString = file.errorString();
}

return( false );
}

Expand Down
2 changes: 1 addition & 1 deletion src/filmslist/filmslistloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FilmsListLoader
{
public:
static bool Populate( FilmItem* rootItem, QString fileName );
static bool Save( FilmItem* rootItem, QString fileName );
static bool Save( FilmItem* rootItem, QString fileName, QString* errorString = nullptr );

static bool CreateEmptyDatabase( QString fileName );

Expand Down
7 changes: 4 additions & 3 deletions src/filmslist/filmslistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,15 @@ QModelIndex FilmsListModel::GetFilmIndex( QString title ) const

void FilmsListModel::SaveToFileSync( QString fileName )
{
if( isDatabaseChanged )
if( isDatabaseChanged && !isReadonly )
{
QMutexLocker locker( &mutexDataEdit );
isDatabaseChanged = false;
QString errorString;

if( !FilmsListLoader::Save( rootItem, fileName ) )
if( !FilmsListLoader::Save( rootItem, fileName, &errorString ) )
{
emit DatabaseWriteError();
emit DatabaseWriteError( errorString );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/filmslist/filmslistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class FilmsListModel : public QAbstractItemModel
signals:
void DatabaseConvertOld();
void DatabaseReadError( QString );
void DatabaseWriteError();
void DatabaseWriteError( QString );
void DatabaseIsEmpty();
void DatabaseIsReadonly();

Expand Down
7 changes: 7 additions & 0 deletions src/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ void MainWindow::DatabaseReadError( const QString& message )
}


void MainWindow::DatabaseWriteError( const QString& message )
{
QMessageBox::critical( this, tr("Database write error"), message );
}


void MainWindow::DatabaseIsEmpty()
{
ClearTextFields();
Expand Down Expand Up @@ -876,6 +882,7 @@ void MainWindow::SetupWindows()

connect( filmsListModel, &FilmsListModel::DatabaseLoaded, this, &MainWindow::DatabaseIsLoaded );
connect( filmsListModel, &FilmsListModel::DatabaseReadError, this, &MainWindow::DatabaseReadError );
connect( filmsListModel, &FilmsListModel::DatabaseWriteError, this, &MainWindow::DatabaseWriteError );
connect( filmsListModel, &FilmsListModel::DatabaseIsReadonly, this, &MainWindow::DatabaseIsReadonly );
connect( filmsListModel, &FilmsListModel::DatabaseIsEmpty, this, &MainWindow::DatabaseIsEmpty );
connect( filmsListModel, &FilmsListModel::DatabaseChanged, this, &MainWindow::DatabaseChanged );
Expand Down
1 change: 1 addition & 0 deletions src/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class MainWindow : public QMainWindow, protected Ui::MainWindow

void DatabaseIsLoaded() { SetAllFunctionsEnabled( true ); }
void DatabaseReadError( const QString& message );
void DatabaseWriteError( const QString& message );
void DatabaseIsEmpty();
void DatabaseIsReadonly();

Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TARGET = alexandra
TEMPLATE = app


#CONFIG += release
CONFIG += release
#CONFIG += debug console

QT = core gui widgets network
Expand Down

0 comments on commit a113b09

Please sign in to comment.