File indexing completed on 2024-04-21 15:12:08

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 2016 Jonathan Marten <jjm@keelhaul.me.uk>     *
0007  *                                  *
0008  *  Kooka is free software; you can redistribute it and/or modify it    *
0009  *  under the terms of the GNU Library General Public License as    *
0010  *  published by the Free Software Foundation and appearing in the  *
0011  *  file COPYING included in the packaging of this file;  either    *
0012  *  version 2 of the License, or (at your option) any later version.    *
0013  *                                  *
0014  *  As a special exception, permission is given to link this program    *
0015  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0016  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0017  *  executable without including the source code for KADMOS in the  *
0018  *  source distribution.                        *
0019  *                                  *
0020  *  This program is distributed in the hope that it will be useful, *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0023  *  GNU General Public License for more details.            *
0024  *                                  *
0025  *  You should have received a copy of the GNU General Public       *
0026  *  License along with this program;  see the file COPYING.  If     *
0027  *  not, see <http://www.gnu.org/licenses/>.                *
0028  *                                  *
0029  ************************************************************************/
0030 
0031 #include "recentsaver.h"
0032 
0033 #include <qurl.h>
0034 #include <qfileinfo.h>
0035 
0036 #include <krecentdirs.h>
0037 
0038 #include "libdialogutil_logging.h"
0039 
0040 
0041 RecentSaver::RecentSaver(const QString &fileClass)
0042 {
0043     Q_ASSERT(!fileClass.isEmpty());
0044     mRecentClass = fileClass;
0045     if (!mRecentClass.startsWith(':')) mRecentClass.prepend(':');
0046 }
0047 
0048 
0049 QUrl RecentSaver::recentUrl(const QString &suggestedName)
0050 {
0051     // QUrl::fromLocalFile("") -> QUrl(), so no need for null test here
0052     return (QUrl::fromLocalFile(recentPath(suggestedName)));
0053 }
0054 
0055 
0056 QString RecentSaver::recentPath(const QString &suggestedName)
0057 {
0058     mRecentDir = KRecentDirs::dir(mRecentClass);
0059     if (!mRecentDir.isEmpty() && !mRecentDir.endsWith('/')) mRecentDir += '/';
0060 
0061     QString recentDir = mRecentDir;
0062     if (!suggestedName.isEmpty()) recentDir += suggestedName;
0063     qCDebug(LIBDIALOGUTIL_LOG) << "for" << mRecentClass << "dir" << mRecentDir << "->" << recentDir;
0064     return (recentDir);
0065 }
0066 
0067 
0068 void RecentSaver::save(const QUrl &url)
0069 {
0070     if (!url.isValid()) return;             // didn't get a valid entry
0071     if (!url.isLocalFile()) return;         // can only save local dirs
0072     save(url.path());                   // save the local path
0073 }
0074 
0075 
0076 void RecentSaver::save(const QString &path)
0077 {
0078     if (path.isEmpty()) return;             // didn't get a valid entry
0079 
0080     QString rd = QFileInfo(path).path();        // just take directory path
0081     if (!rd.endsWith('/')) rd += '/';           // ensure saved as directory
0082     if (rd==mRecentDir) return;             // nothing new, no need to save
0083 
0084     qCDebug(LIBDIALOGUTIL_LOG) << "for" << mRecentClass << "saving" << rd;
0085     KRecentDirs::add(mRecentClass, rd);
0086 }