File indexing completed on 2024-05-19 13:33:01

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "filemetainfo.h"
0012 #include "textdocument.h"
0013 #include "textview.h"
0014 
0015 #include <KConfig>
0016 #include <KConfigGroup>
0017 
0018 #include <QStandardPaths>
0019 
0020 // BEGIN class MetaInfo
0021 MetaInfo::MetaInfo()
0022 {
0023     m_cursorLine = 0;
0024     m_cursorColumn = 0;
0025 }
0026 
0027 bool MetaInfo::hasDefaultData() const
0028 {
0029     return bookmarks().isEmpty() && breakpoints().isEmpty() && (m_outputMethodInfo.method() == OutputMethodInfo::Method::Direct) && (m_cursorLine == 0) && (m_cursorColumn == 0);
0030 }
0031 
0032 void MetaInfo::save(KConfigGroup *conf)
0033 {
0034     conf->writeEntry("Bookmarks", bookmarks());
0035     conf->writeEntry("Breakpoints", breakpoints());
0036     conf->writeEntry("OutputMethod", toID(outputMethodInfo().method()));
0037     conf->writePathEntry("OutputPath", outputMethodInfo().outputFile().toDisplayString(QUrl::PreferLocalFile));
0038     conf->writeEntry("OutputPicID", outputMethodInfo().picID());
0039     conf->writeEntry("CursorLine", cursorLine());
0040     conf->writeEntry("CursorColumn", cursorColumn());
0041 }
0042 
0043 void MetaInfo::load(KConfigGroup *conf)
0044 {
0045     setBookmarks(conf->readEntry("Bookmarks", IntList()));
0046     setBreakpoints(conf->readEntry("Breakpoints", IntList()));
0047     m_outputMethodInfo.setMethod(toMethod(conf->readEntry("OutputMethod")));
0048     m_outputMethodInfo.setOutputFile(QUrl::fromUserInput(conf->readPathEntry("OutputPath", QString())));
0049     m_outputMethodInfo.setPicID(conf->readEntry("OutputPicID"));
0050     setCursorLine(conf->readEntry("CursorLine", 0));
0051     setCursorColumn(conf->readEntry("CursorColumn", 0));
0052 }
0053 
0054 OutputMethodInfo::Method::Type MetaInfo::toMethod(const QString &id)
0055 {
0056     if (id == "SaveAndLoad")
0057         return OutputMethodInfo::Method::SaveAndLoad;
0058 
0059     else if (id == "SaveAndForget")
0060         return OutputMethodInfo::Method::SaveAndForget;
0061 
0062     return OutputMethodInfo::Method::Direct;
0063 }
0064 
0065 QString MetaInfo::toID(OutputMethodInfo::Method::Type method)
0066 {
0067     switch (method) {
0068     case OutputMethodInfo::Method::SaveAndLoad:
0069         return "SaveAndLoad";
0070 
0071     case OutputMethodInfo::Method::SaveAndForget:
0072         return "SaveAndForget";
0073 
0074     case OutputMethodInfo::Method::Direct:
0075     default:
0076         return "Direct";
0077     }
0078 }
0079 // END class MetaInfo
0080 
0081 // BEGIN class FileMetaInfo
0082 FileMetaInfo::FileMetaInfo()
0083     : QObject()
0084 {
0085     // m_metaInfoConfig = new KConfig( "metainfo", false, false, "appdata" );
0086     m_metaInfoConfig = new KConfig("metainfo", KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
0087     loadAllMetaInfo();
0088 }
0089 
0090 FileMetaInfo::~FileMetaInfo()
0091 {
0092     saveAllMetaInfo();
0093     delete m_metaInfoConfig;
0094 }
0095 
0096 void FileMetaInfo::grabMetaInfo(const QUrl &url, TextDocument *textDocument)
0097 {
0098     if (!textDocument)
0099         return;
0100 
0101     m_metaInfoMap[url].setBookmarks(textDocument->bookmarkList());
0102     m_metaInfoMap[url].setBreakpoints(textDocument->breakpointList());
0103 }
0104 
0105 void FileMetaInfo::initializeFromMetaInfo(const QUrl &url, TextDocument *textDocument)
0106 {
0107     if (!textDocument)
0108         return;
0109 
0110     textDocument->setBookmarks(m_metaInfoMap[url].bookmarks());
0111     textDocument->setBreakpoints(m_metaInfoMap[url].breakpoints());
0112 }
0113 
0114 void FileMetaInfo::grabMetaInfo(const QUrl &url, TextView *textView)
0115 {
0116     if (!textView)
0117         return;
0118 
0119     m_metaInfoMap[url].setCursorLine(textView->currentLine());
0120     m_metaInfoMap[url].setCursorColumn(textView->currentColumn());
0121 }
0122 
0123 void FileMetaInfo::initializeFromMetaInfo(const QUrl &url, TextView *textView)
0124 {
0125     if (!textView)
0126         return;
0127 
0128     textView->setCursorPosition(m_metaInfoMap[url].cursorLine(), m_metaInfoMap[url].cursorColumn());
0129 }
0130 
0131 void FileMetaInfo::grabMetaInfo(const QUrl &url, OutputMethodDlg *dlg)
0132 {
0133     if (!dlg)
0134         return;
0135 
0136     m_metaInfoMap[url].setOutputMethodInfo(dlg->info());
0137 }
0138 
0139 void FileMetaInfo::initializeFromMetaInfo(const QUrl &url, OutputMethodDlg *dlg)
0140 {
0141     if (!dlg || url.isEmpty() || !m_metaInfoMap.contains(url))
0142         return;
0143 
0144     OutputMethodInfo::Method::Type method = m_metaInfoMap[url].outputMethodInfo().method();
0145     dlg->setMethod(method);
0146 
0147     if (method != OutputMethodInfo::Method::Direct)
0148         dlg->setOutputFile(m_metaInfoMap[url].outputMethodInfo().outputFile());
0149 
0150     dlg->setPicID(m_metaInfoMap[url].outputMethodInfo().picID());
0151 }
0152 
0153 void FileMetaInfo::saveAllMetaInfo()
0154 {
0155     const MetaInfoMap::iterator end = m_metaInfoMap.end();
0156     for (MetaInfoMap::iterator it = m_metaInfoMap.begin(); it != end; ++it) {
0157         if (it.value().hasDefaultData())
0158             m_metaInfoConfig->deleteGroup(it.key().toDisplayString());
0159 
0160         else {
0161             KConfigGroup grUrl = m_metaInfoConfig->group(it.key().toDisplayString());
0162             it.value().save(&grUrl);
0163         }
0164     }
0165 }
0166 
0167 void FileMetaInfo::loadAllMetaInfo()
0168 {
0169     QStringList urlList = m_metaInfoConfig->groupList();
0170     const QStringList::iterator end = urlList.end();
0171     for (QStringList::iterator it = urlList.begin(); it != end; ++it) {
0172         KConfigGroup grUrl = m_metaInfoConfig->group(*it);
0173         m_metaInfoMap[QUrl::fromUserInput(*it)].load(&grUrl);
0174     }
0175 }
0176 // END class FileMetaInfo
0177 
0178 #include "moc_filemetainfo.cpp"