File indexing completed on 2024-04-28 16:01:31

0001 /******************************************************************************
0002  * This file is part of the libqgit2 library
0003  * Copyright (c) 2012 Laszlo Papp <djszapi@archlinux.us>
0004  * Copyright (C) 2013 Leonardo Giordani
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, write to the Free Software
0018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0019  */
0020 
0021 #include "qgitconfig.h"
0022 
0023 #include "qgitexception.h"
0024 #include "qgitrepository.h"
0025 #include "private/buffer.h"
0026 #include "private/pathcodec.h"
0027 
0028 namespace LibQGit2
0029 {
0030 
0031 Config::Config(git_config *cfg)
0032     : d(cfg)
0033 {
0034     if (d == 0)
0035         git_config_new(&d);
0036 }
0037 
0038 Config::Config(const Config &other)
0039     : d(other.d)
0040 {
0041 }
0042 
0043 Config::~Config()
0044 {
0045     git_config_free(d);
0046 }
0047 
0048 Config Config::fromGlobalConfig()
0049 {
0050     git_config * def;
0051     git_config * cfg;
0052     git_config_open_default(&def);
0053     if ( git_config_open_global(&cfg, def) == GIT_OK )
0054         return Config(cfg);
0055 
0056     return Config();
0057 }
0058 
0059 QString Config::findGlobal()
0060 {
0061     internal::Buffer buffer;
0062     qGitThrow(git_config_find_global(buffer.data()));
0063 
0064     return buffer.asPath();
0065 }
0066 
0067 QString Config::findSystem()
0068 {
0069     internal::Buffer buffer;
0070     qGitThrow(git_config_find_system(buffer.data()));
0071 
0072     return buffer.asPath();
0073 }
0074 
0075 bool Config::append(const QString &path, git_config_level_t level, const Repository &repo, bool force)
0076 {
0077     return GIT_OK == git_config_add_file_ondisk(d, PathCodec::toLibGit2(path).constData(), level, repo.constData(), force);
0078 }
0079 
0080 QVariant Config::value(const QString &key, const QVariant &defaultValue) const
0081 {
0082     const char * result = 0;
0083     if (git_config_get_string(&result, d, key.toUtf8().constData()) == GIT_OK)
0084         return QString::fromUtf8(result);
0085 
0086     return defaultValue;
0087 }
0088 
0089 void Config::setValue(const QString &key, const QVariant &value)
0090 {
0091     qGitThrow( git_config_set_string(d, key.toUtf8(), value.toString().toUtf8().constData()) );
0092 }
0093 
0094 
0095 } // LibQGit2
0096