File indexing completed on 2024-04-21 15:55:29
0001 /******************************************************************************** 0002 * Copyright (C) 2007 by Holger Danielsson (holger.danielsson@versanet.de) * 0003 * 2008 - 2010 by Michel Ludwig (michel.ludwig@kdemail.net) * 0004 *********************************************************************************/ 0005 0006 /************************************************************************** 0007 * * 0008 * This program is free software; you can redistribute it and/or modify * 0009 * it under the terms of the GNU General Public License as published by * 0010 * the Free Software Foundation; either version 2 of the License, or * 0011 * (at your option) any later version. * 0012 * * 0013 ***************************************************************************/ 0014 0015 #include "abbreviationmanager.h" 0016 0017 #include <KMessageBox> 0018 #include "codecompletion.h" 0019 #include "kileinfo.h" 0020 #include "utilities.h" 0021 0022 namespace KileAbbreviation { 0023 0024 Manager::Manager(KileInfo* kileInfo, QObject *parent) : QObject(parent), m_kileInfo(kileInfo), m_abbreviationsDirty(false) 0025 { 0026 setObjectName("KileAbbreviation::Manager"); 0027 m_localAbbreviationFile = KileUtilities::writableLocation(QStandardPaths::AppDataLocation) + '/' + "complete/abbreviation/" + "kile-abbrevs.cwl"; 0028 QDir testDir(m_localAbbreviationFile); 0029 if (!testDir.exists()) { 0030 testDir.mkpath(m_localAbbreviationFile); 0031 } 0032 } 0033 0034 Manager::~Manager() 0035 { 0036 } 0037 0038 const AbbreviationMap& Manager::getAbbreviationMap() 0039 { 0040 return m_abbreviationMap; 0041 } 0042 0043 void Manager::updateLocalAbbreviation(const QString& text, const QString& replacement) 0044 { 0045 if(text.isEmpty() || replacement.isEmpty()) { 0046 return; 0047 } 0048 AbbreviationMap::iterator it = m_abbreviationMap.find(text); 0049 if(it != m_abbreviationMap.end()) { 0050 StringBooleanPair pair = it.value(); 0051 if(pair.first == replacement) { 0052 return; 0053 } 0054 m_abbreviationMap.erase(it); 0055 } 0056 m_abbreviationMap[text] = createLocalAbbreviationPair(replacement); 0057 m_abbreviationsDirty = true; 0058 emit(abbreviationsChanged()); 0059 } 0060 0061 void Manager::removeLocalAbbreviation(const QString& text) 0062 { 0063 AbbreviationMap::iterator it = m_abbreviationMap.find(text); 0064 if(it == m_abbreviationMap.end()) { 0065 return; 0066 } 0067 StringBooleanPair pair = it.value(); 0068 if(isLocalAbbreviation(pair)) { 0069 m_abbreviationMap.erase(it); 0070 m_abbreviationsDirty = true; 0071 } 0072 emit(abbreviationsChanged()); 0073 } 0074 0075 void Manager::readAbbreviationFiles() 0076 { 0077 if(m_abbreviationsDirty) { 0078 saveLocalAbbreviations(); 0079 } 0080 m_abbreviationMap.clear(); 0081 QStringList list = m_kileInfo->codeCompletionManager()->readCWLFiles(KileConfig::completeAbbrev(), "abbreviation"); 0082 addAbbreviationListToMap(list, true); 0083 0084 // read local wordlist 0085 list = m_kileInfo->codeCompletionManager()->readCWLFile(m_localAbbreviationFile, true); 0086 addAbbreviationListToMap(list, false); 0087 0088 emit(abbreviationsChanged()); 0089 } 0090 0091 void Manager::saveLocalAbbreviations() 0092 { 0093 if(!m_abbreviationsDirty) { 0094 return; 0095 } 0096 0097 KILE_DEBUG_MAIN; 0098 // create the file 0099 QFile abbreviationFile(m_localAbbreviationFile); 0100 if(!abbreviationFile.open(QIODevice::WriteOnly)) { 0101 KMessageBox::error(m_kileInfo->mainWindow(), i18n("Could not save the local abbreviation list.\nError code %1.", QString::number(abbreviationFile.error())), 0102 i18n("Saving Problem")); 0103 return; 0104 } 0105 0106 QTextStream stream(&abbreviationFile); 0107 stream << "# abbreviation mode: editable abbreviations\n"; 0108 0109 //QTextCodec *codec = QTextCodec::codecForName(m_ki->activeTextDocument()->encoding().ascii()); 0110 // stream.setCodec(codec); 0111 0112 for(AbbreviationMap::iterator i = m_abbreviationMap.begin(); 0113 i != m_abbreviationMap.end(); ++i) { 0114 StringBooleanPair pair = i.value(); 0115 if(!pair.second) { 0116 stream << QString(i.key()).replace('=', "\\=") 0117 << '=' << pair.first << '\n'; 0118 } 0119 } 0120 abbreviationFile.close(); 0121 0122 m_abbreviationsDirty = false; 0123 } 0124 0125 void Manager::addAbbreviationListToMap(const QStringList& list, bool global) 0126 { 0127 // a '=' symbol in the left-hand side is encoded by '\=' 0128 for(QStringList::const_iterator i = list.begin(); i != list.end(); ++i) { 0129 QString entry = *i; 0130 int delimiter = entry.indexOf(QRegExp("[^\\\\]=")); 0131 if(delimiter < 0) { 0132 continue; 0133 } 0134 QString left = entry.left(delimiter + 1); // [^\\\\]= has length 2. 0135 left.replace("\\=", "="); 0136 QString right = entry.mid(delimiter + 2); // [^\\\\]= has length 2. 0137 if(right.isEmpty()) { 0138 continue; 0139 } 0140 m_abbreviationMap[left] = StringBooleanPair(right, global); 0141 } 0142 } 0143 0144 QStringList Manager::getAbbreviationTextMatches(const QString& text) const 0145 { 0146 QStringList toReturn; 0147 for(AbbreviationMap::const_iterator i = m_abbreviationMap.begin(); 0148 i != m_abbreviationMap.end(); ++i) { 0149 if(i.key().startsWith(text)) { 0150 toReturn.append(i.value().first); 0151 } 0152 } 0153 return toReturn; 0154 } 0155 0156 QString Manager::getAbbreviationTextMatch(const QString& text) const 0157 { 0158 return m_abbreviationMap[text].first; 0159 } 0160 0161 bool Manager::abbreviationStartsWith(const QString& text) const 0162 { 0163 for(AbbreviationMap::const_iterator i = m_abbreviationMap.begin(); 0164 i != m_abbreviationMap.end(); ++i) { 0165 if(i.key().startsWith(text)) { 0166 return true; 0167 } 0168 } 0169 return false; 0170 } 0171 0172 bool Manager::isAbbreviationDefined(const QString& text) const 0173 { 0174 return m_abbreviationMap.find(text) != m_abbreviationMap.end(); 0175 } 0176 0177 0178 } 0179