c2bUtils.cpp 1.28 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2004-2018 by Pere Constans
3
 *   constans@molspaces.com
4
 *   cb2Bib version 1.9.9. Licensed under the GNU GPL version 3.
5 6 7 8 9 10 11 12 13 14 15 16 17
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bUtils.h"

#include <QDesktopServices>
#include <QMessageBox>


namespace c2bUtils
{

bool openFile(const QString& fn, QWidget* w)
{
18
    // URL fn is decoded always, in the hope of making openFile encoding agnostic
19 20
    const QString decoded(QUrl::fromPercentEncoding(fn.toUtf8()));
    const QUrl url(stringToUrl(decoded));
21 22 23 24
    if (!url.isValid())
    {
        if (w)
            QMessageBox::warning(w, QObject::tr("Warning - cb2Bib"),
25
                                 QObject::tr("Unable to open file.\nError: URL '%1' is not valid.").
26 27 28 29 30 31 32 33 34 35 36 37 38 39
                                 arg(url.toString()), QMessageBox::Ok);
        return false;
    }
    if (!QDesktopServices::openUrl(url))
    {
        if (w)
            QMessageBox::warning(w, QObject::tr("Warning - cb2Bib"), QObject::tr("Unable to open file '%1'.").
                                 arg(fn), QMessageBox::Ok);
        return false;
    }
    return true;
}

} // namespace c2bUtils