GuiProjectManager.cpp 10.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * GUIProjectManager.cpp: Handle the GUI part of the project managing
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "GuiProjectManager.h"

25
#include "Main/Core.h"
26 27 28 29
#include "Library/Library.h"
#include "Workflow/MainWorkflow.h"
#include "Settings/SettingsManager.h"
#include "Project/Workspace.h"
30

31 32
#include "timeline/Timeline.h"

33 34 35 36
#include <QDateTime>
#include <QFileDialog>
#include <QMessageBox>
#include <QTimer>
37 38 39 40 41

GUIProjectManager::GUIProjectManager()
{
    connect( this, SIGNAL( projectClosed() ), Library::getInstance(), SLOT( clear() ) );
    connect( this, SIGNAL( projectClosed() ), MainWorkflow::getInstance(), SLOT( clear() ) );
42 43
    connect( Library::getInstance(), SIGNAL( cleanStateChanged( bool ) ),
             this, SLOT( cleanChanged( bool ) ) );
44 45 46 47

    //Automatic save part :
    m_timer = new QTimer( this );
    connect( m_timer, SIGNAL( timeout() ), this, SLOT( autoSaveRequired() ) );
48
    VLMC_CREATE_PREFERENCE_BOOL( "vlmc/AutomaticBackup", false,
49 50 51 52
                                 QT_TRANSLATE_NOOP( "PreferenceWidget", "Automatic save" ),
                                 QT_TRANSLATE_NOOP( "PreferenceWidget", "When this option is activated,"
                                             "VLMC will automatically save your project "
                                             "at a specified interval" ) );
53
    Core::getInstance()->settings()->watchValue( "vlmc/AutomaticBackup", this,
54 55 56
                                                SLOT( automaticSaveEnabledChanged(QVariant) ),
                                                SettingsManager::Vlmc,
                                                Qt::QueuedConnection );
57
    VLMC_CREATE_PREFERENCE_INT( "vlmc/AutomaticBackupInterval", 5,
58 59 60
                                QT_TRANSLATE_NOOP( "PreferenceWidget", "Automatic save interval" ),
                                QT_TRANSLATE_NOOP( "PreferenceWidget", "This is the interval that VLMC will wait "
                                            "between two automatic save" ) );
61
    Core::getInstance()->settings()->watchValue( "vlmc/AutomaticBackupInterval", this,
62 63 64
                                                SLOT( automaticSaveIntervalChanged(QVariant) ),
                                                SettingsManager::Vlmc,
                                                Qt::QueuedConnection );
65
    automaticSaveEnabledChanged( VLMC_GET_BOOL( "vlmc/AutomaticBackup" ) );
66
    Core::getInstance()->settings()->watchValue( "vlmc/ProjectName", this,
67 68
                                                SLOT(projectNameChanged(QVariant) ),
                                                SettingsManager::Project );
69 70 71 72 73 74 75 76 77
}

bool
GUIProjectManager::askForSaveIfModified()
{
    if ( m_needSave == true )
    {
        QMessageBox msgBox;
        msgBox.setText( tr( "The project has been modified." ) );
78
        msgBox.setInformativeText( tr( "Do you want to save it?" ) );
79
        msgBox.setStandardButtons( QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel );
80
        msgBox.setDefaultButton( QMessageBox::Save );
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        int     ret = msgBox.exec();

        switch ( ret )
        {
            case QMessageBox::Save:
                saveProject();
                break ;
            case QMessageBox::Discard:
                break ;
            case QMessageBox::Cancel:
            default:
                return false ;
        }
    }
    return true;
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
bool
GUIProjectManager::confirmRelocate() const
{
    QMessageBox msgBox;
    msgBox.setText( tr( "You are about to relocate the project. Every video will be copied to your new workspace." ) );
    msgBox.setInformativeText( tr( "Do you want to proceed?" ) );
    msgBox.setStandardButtons( QMessageBox::Ok | QMessageBox::No );
    msgBox.setDefaultButton( QMessageBox::Ok );
    int     ret = msgBox.exec();

    switch ( ret )
    {
    case QMessageBox::Ok:
        return true;
    case QMessageBox::No:
        return false ;
    default:
        return false;
    }
}

119 120 121 122 123
bool
GUIProjectManager::createNewProjectFile( bool saveAs )
{
    if ( m_projectFile == NULL || saveAs == true )
    {
124 125
        bool        relocate = false;

126
        QString defaultPath = VLMC_PROJECT_GET_STRING( "vlmc/Workspace" );
127
        if ( defaultPath.length() == 0 )
128
            defaultPath = VLMC_GET_STRING( "vlmc/DefaultProjectLocation" );
129
        QString outputFileName =
130 131
            QFileDialog::getSaveFileName( NULL, tr( "Enter the output file name" ),
                                          defaultPath, tr( "VLMC project file(*.vlmc)" ) );
132 133
        if ( outputFileName.length() == 0 )
            return false;
134 135 136 137 138 139
        if ( Workspace::isInProjectDir( outputFileName ) == false )
        {
            if ( confirmRelocate() == false )
                return false;
            relocate = true;
        }
140 141 142 143 144
        if ( m_projectFile != NULL )
            delete m_projectFile;
        if ( outputFileName.endsWith( ".vlmc" ) == false )
            outputFileName += ".vlmc";
        m_projectFile = new QFile( outputFileName );
145
        QFileInfo       fInfo( outputFileName );
146
        Core::getInstance()->settings()->setValue( "vlmc/Workspace",
147 148
                                                  fInfo.absolutePath(), SettingsManager::Project);

149
        appendToRecentProject( projectName() );
150 151
        if ( relocate == true )
            Workspace::getInstance()->copyAllToWorkspace();
152 153 154 155 156 157
        emit projectUpdated( projectName(), true );
    }
    return true;
}

void
158
GUIProjectManager::saveProject( bool saveAs /* = false */ )
159 160 161 162 163
{
    //If the project is still unsaved, or if we want to
    //save the project with a new name
    if ( createNewProjectFile( saveAs ) == false )
        return ;
164
    ProjectManager::saveAs( outputFileName() );
165 166 167 168 169 170 171
}

bool
GUIProjectManager::closeProject()
{
    if ( askForSaveIfModified() == false )
        return false;
172 173 174 175
    bool ret = ProjectManager::closeProject();
    //This one is for the mainwindow, to update the title bar
    emit projectUpdated( projectName(), true );
    return ret;
176 177 178
}

void
179
GUIProjectManager::newProject( const QString &projectName, const QString &workspacePath )
180 181 182 183
{
    if ( closeProject() == false )
        return ;
    m_projectName = projectName;
184 185
    //Current project file has already been delete/nulled by ProjectManager::closeProject()
    m_projectFile = new QFile( workspacePath + '/' + "project.vlmc" );
186
    saveProject( false );
187
    appendToRecentProject( projectName );
188 189 190 191 192 193 194 195
}


void
GUIProjectManager::autoSaveRequired()
{
    if ( m_projectFile == NULL )
        return ;
196
    ProjectManager::__saveProject( createAutoSaveOutputFileName( m_projectFile->fileName() ) );
197 198 199 200 201 202 203 204 205
}

void
GUIProjectManager::automaticSaveEnabledChanged( const QVariant& val )
{
    bool    enabled = val.toBool();

    if ( enabled == true )
    {
206
        int interval = VLMC_GET_INT( "vlmc/AutomaticBackupInterval" );
207 208 209 210 211 212 213 214 215
        m_timer->start( interval * 1000 * 60 );
    }
    else
        m_timer->stop();
}

void
GUIProjectManager::automaticSaveIntervalChanged( const QVariant& val )
{
216
    bool enabled = VLMC_GET_BOOL( "vlmc/AutomaticBackup" );
217 218 219 220 221

    if ( enabled == false )
        return ;
    m_timer->start( val.toInt() * 1000 * 60 );
}
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

bool
GUIProjectManager::needSave() const
{
    return m_needSave;
}

void
GUIProjectManager::cleanChanged( bool val )
{
    m_needSave = !val;
    emit projectUpdated( projectName(), val );
}


void
GUIProjectManager::projectNameChanged( const QVariant& name )
{
    m_projectName = name.toString();
    emit projectUpdated( m_projectName, !m_needSave );
}
243 244 245 246 247 248

void
GUIProjectManager::failedToLoad( const QString &reason ) const
{
    QMessageBox::warning( NULL, tr( "Failed to load project file" ), reason );
}
249 250 251 252 253 254

void
GUIProjectManager::saveTimeline( QXmlStreamWriter &project )
{
    Timeline::getInstance()->save( project );
}
255 256 257 258 259 260

void
GUIProjectManager::loadTimeline( const QDomElement &root )
{
    Timeline::getInstance()->load( root );
}
261

262
void
263
GUIProjectManager::loadProject( const QString &fileName )
264
{
265 266 267 268 269
    QFile   projectFile( fileName );
    //If for some reason this happens... better safe than sorry
    if ( projectFile.exists() == false )
        return ;

270
    QString fileToLoad = fileName;
271 272 273 274 275 276 277 278 279 280 281
    QString backupFilename = createAutoSaveOutputFileName( fileName );
    QFile   autoBackup( backupFilename );
    if ( autoBackup.exists() == true )
    {
        QFileInfo       projectFileInfo( projectFile );
        QFileInfo       autobackupFileInfo( autoBackup );

        if ( autobackupFileInfo.lastModified() > projectFileInfo.lastModified() )
        {
            if ( QMessageBox::question( NULL, tr( "Backup file" ),
                                        tr( "A backup file exists for this project. "
Nikoli's avatar
Nikoli committed
282
                                        "Do you want to load it?" ),
283 284
                                        QMessageBox::Ok | QMessageBox::No ) == QMessageBox::Ok )
            {
285
                fileToLoad = backupFilename;
286 287 288 289 290 291
            }
        }
        else
        {
            if ( QMessageBox::question( NULL, tr( "Backup file" ),
                                        tr( "An outdated backup file was found. "
Nikoli's avatar
Nikoli committed
292
                                       "Do you want to erase it?" ),
293 294 295 296 297 298
                                        QMessageBox::Ok | QMessageBox::No ) == QMessageBox::Ok )
            {
                autoBackup.remove();
            }
        }
    }
299 300 301 302 303 304 305
    ProjectManager::loadProject( fileToLoad );
}

void
GUIProjectManager::loadProject()
{
    QString fileName =
306
            QFileDialog::getOpenFileName( NULL, tr( "Please choose a project file" ),
307
                                            VLMC_GET_STRING( "vlmc/DefaultProjectLocation" ),
308
                                            tr( "VLMC project file(*.vlmc)" ) );
309 310 311
    if ( fileName.length() <= 0 ) //If the user canceled.
        return ;
    loadProject( fileName );
312
}