Make the passing of lots of output to the pager have immediate result on screen
[vng.git] / Configuration.cpp
blob36020abdb2eaf6cc9841451f85ec71fc5146ae0b
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "Configuration.h"
19 #include "Logger.h"
21 #include <QDebug>
23 #ifndef Q_OS_WIN
24 #include <unistd.h>
25 #endif
27 Configuration::Configuration(const QString &section)
28 : m_repoDir("."),
29 m_section(section),
30 m_dirty(true),
31 m_emptyRepo(false)
35 bool Configuration::contains(const QString & key) const
37 const_cast<Configuration*> (this)->readConfig();
38 return m_options.contains(key);
41 QDir Configuration::repository() const
43 const_cast<Configuration*> (this)->readConfig();
44 return m_repoDir;
47 void Configuration::readConfig()
49 if (!m_dirty)
50 return;
51 m_dirty = false;
52 QObject deleterParent;
54 QDir dir = QDir::current();
55 do {
56 QDir git = dir;
57 if (git.cd(".git")) {
58 m_repoDir = dir;
59 QDir refs(git.absoluteFilePath("refs/heads"));
60 m_emptyRepo = refs.count() == 2; // only '.' and '..'
61 break;
63 if (!dir.cdUp())
64 break;
65 } while(!dir.isRoot());
67 QString home = QDir::homePath();
68 QFile *config;
69 config = new QFile(home + "/.vng/config", &deleterParent);
70 if (! config->exists())
71 config = new QFile(home + "/.darcs/defaults", &deleterParent);
72 if (config->exists()) {
73 if (! config->open(QIODevice::ReadOnly)) {
74 Logger::error() << "Failed to open config file, is it readable?\n";
75 return;
78 char buf[1024];
79 while(true) {
80 qint64 lineLength = config->readLine(buf, sizeof(buf));
81 if (lineLength == -1)
82 break;
83 QString line = QString::fromUtf8(buf, lineLength);
84 QString option;
85 if (line.startsWith("ALL "))
86 option = line.mid(3).trimmed();
87 else if (line.length() > m_section.length() && line.startsWith(m_section))
88 option = line.mid(m_section.length()).trimmed();
89 if (! option.isEmpty()) {
90 const int index = option.indexOf(' ');
91 if (index > 0) {
92 m_options.insert(option.left(index).trimmed(), option.mid(index).trimmed());
94 else
95 m_options.insert(option, QString());
98 config->close();
102 QString Configuration::optionArgument(const QString &optionName, const QString &defaultValue) const
104 if (m_options.contains(optionName))
105 return m_options[optionName];
106 return defaultValue;
109 bool Configuration::colorTerm() const
111 #ifndef Q_OS_WIN
112 if (isatty(1))
113 return QString(getenv("TERM")) != QString("dumb") && !Logger::hasNonColorPager();
114 #endif
115 return false;
118 bool Configuration::isEmptyRepo() const
120 const_cast<Configuration*> (this)->readConfig();
121 return m_emptyRepo;