namespace_def.cpp

Go to the documentation of this file.
00001 /*
00002         Author: Marco Costalba (C) 2005-2006
00003 
00004         Copyright: See COPYING file that comes with this distribution
00005 
00006 
00007  Definitions of complex namespace constants
00008 
00009  Complex constant objects are not folded in like integral types, so they
00010  are declared 'extern' in namespace to avoid duplicating them as file scope
00011  data in each file where QGit namespace is included.
00012 
00013 */
00014 #include <sys/types.h> // used by chmod()
00015 #include <sys/stat.h>  // used by chmod()
00016 #include <qsettings.h>
00017 #include <qdir.h>
00018 #include <qfile.h>
00019 #include "common.h"
00020 
00021 // minimum git version required
00022 const QString QGit::GIT_VERSION = "1.4.4";
00023 
00024 // colors
00025 const QColor QGit::BROWN       = QColor(150, 75, 0);
00026 const QColor QGit::ORANGE      = QColor(255, 160, 50);
00027 const QColor QGit::DARK_ORANGE = QColor(216, 144, 0);
00028 const QColor QGit::LIGHT_ORANGE = QColor(255, 221, 170);
00029 const QColor QGit::LIGHT_BLUE  = QColor(85, 255, 255);
00030 const QColor QGit::PURPLE      = QColor(221, 221, 255);
00031 const QColor QGit::DARK_GREEN  = QColor(0, 205, 0);
00032 
00033 // initialized at startup according to system wide settings
00034 QColor QGit::ODD_LINE_COL;
00035 QColor QGit::EVEN_LINE_COL;
00036 
00037 /*
00038    Default QFont c'tor calls static method QApplication::font() that could
00039    be still NOT initialized at this time, so set a dummy font family instead,
00040    it will be properly changed later, at startup
00041 */
00042 QFont QGit::TYPE_WRITER_FONT("Helvetica");
00043 
00044 // patches drag and drop
00045 const QString QGit::PATCHES_DIR  = "/.qgit_patches_copy";
00046 const QString QGit::PATCHES_NAME = "qgit_import";
00047 
00048 // git index parameters
00049 const QString QGit::ZERO_SHA        = "0000000000000000000000000000000000000000";
00050 const QString QGit::CUSTOM_SHA      = "CUSTOM";
00051 const QString QGit::ALL_MERGE_FILES = "ALL_MERGE_FILES";
00052 
00053 // settings keys
00054 const QString QGit::APP_KEY         = "/qgit/";
00055 const QString QGit::FP_DIR_KEY      = "format_patch_last_dir";
00056 const QString QGit::FPATCH_ARGS_KEY = "format_patch_args";
00057 const QString QGit::FLAGS_KEY       = "patch_flags";
00058 const QString QGit::CMT_GEOM_KEY    = "commit_viewer_geometry";
00059 const QString QGit::CMT_SPLIT_KEY   = "commit_viewer_splitter_sizes";
00060 const QString QGit::CMT_TEMPL_KEY   = "commit_template_file_path";
00061 const QString QGit::CMT_ARGS_KEY    = "commit_args";
00062 const QString QGit::EX_KEY          = "exclude_file_path";
00063 const QString QGit::EX_PER_DIR_KEY  = "exclude_per_directory_file_name";
00064 const QString QGit::EXT_DIFF_KEY    = "external_diff_viewer";
00065 const QString QGit::REC_REP_KEY     = "recent_open_repos";
00066 const QString QGit::MCR_NAME_KEY    = "macro_name";
00067 const QString QGit::MCR_TEXT_KEY    = "commands";
00068 const QString QGit::MCR_LIST_KEY    = "macro_list";
00069 const QString QGit::FONT_KEY        = "typewriter_font";
00070 
00071 // settings default values
00072 const QString QGit::CMT_GEOM_DEF    = "290,140,495,540";
00073 const QString QGit::CMT_SPLIT_DEF   = "155,342";
00074 const QString QGit::CMT_TEMPL_DEF   = ".git/commit-template";
00075 const QString QGit::EX_DEF          = ".git/info/exclude";
00076 const QString QGit::EX_PER_DIR_DEF  = ".gitignore";
00077 const QString QGit::EXT_DIFF_DEF    = "kompare";
00078 const QString QGit::MCR_NAME_DEF    = "New macro";
00079 
00080 // cache file
00081 const QString QGit::BAK_EXT          = ".bak";
00082 const QString QGit::C_DAT_FILE       = "/qgit_cache.dat";
00083 
00084 // misc
00085 const QString QGit::QUOTE_CHAR = "$";
00086 
00087 using namespace QGit;
00088 
00089 // settings helpers
00090 uint QGit::flags(SCRef group) {
00091 
00092         QSettings settings;
00093         return settings.readNumEntry(APP_KEY + group + FLAGS_KEY, FLAGS_DEF);
00094 }
00095 
00096 bool QGit::testFlag(uint f, SCRef group) {
00097 
00098         return (flags(group) & f);
00099 }
00100 
00101 void QGit::setFlag(uint f, bool b, SCRef group) {
00102 
00103         QSettings settings;
00104         int flags = settings.readNumEntry(APP_KEY + group + FLAGS_KEY, FLAGS_DEF);
00105         flags = (b) ? flags | f : flags & ~f;
00106         settings.writeEntry(APP_KEY + group + FLAGS_KEY, flags);
00107 }
00108 
00109 void QGit::writeSetting(SCRef key, SCRef value, SCRef group) {
00110 
00111         QSettings settings;
00112         settings.writeEntry(APP_KEY + group + key, value);
00113 }
00114 
00115 // misc helpers
00116 bool QGit::stripPartialParaghraps(const QByteArray& ba, QString* dst, QString* prev) {
00117 
00118         QString src(ba);
00119         // handle rare case of a '\0' inside content
00120         while (src.length() < ba.size() && ba.at(src.length()) == '\0') {
00121                 int start = src.length() + 1;
00122                 QByteArray tail;
00123                 tail.duplicate(ba.data() + start, ba.size() - start);
00124                 src.append(" ").append(tail); // a space instead of '\0' so sizes match
00125         }
00126         int idx = src.findRev('\n');
00127         if (idx == -1) {
00128                 prev->append(src);
00129                 *dst = "";
00130                 return false;
00131         }
00132         *dst = src.left(idx).prepend(*prev); // strip trailing '\n'
00133         *prev = src.mid(idx + 1); // src[idx] is '\n', skip it
00134         return true;
00135 }
00136 
00137 bool QGit::writeToFile(SCRef fileName, SCRef data, bool setExecutable) {
00138 
00139         QFile file(fileName);
00140         if (!file.open(IO_WriteOnly)) {
00141                 dbp("ERROR: unable to write file %1", fileName);
00142                 return false;
00143         }
00144         QTextStream stream(&file);
00145         stream << data;
00146         file.close();
00147         if (setExecutable) {
00148                 if (chmod(fileName, 0755)) {
00149                         dbp("ERROR: unable to set executable bit to file %1", fileName);
00150                         QDir d;
00151                         d.remove(fileName);
00152                         return false;
00153                 }
00154         }
00155         return true;
00156 }
00157 
00158 bool QGit::writeToFile(SCRef fileName, const QByteArray& data, bool setExecutable) {
00159 
00160         QFile file(fileName);
00161         if (!file.open(IO_WriteOnly)) {
00162                 dbp("ERROR: unable to write file %1", fileName);
00163                 return false;
00164         }
00165         QDataStream stream(&file);
00166         stream.writeRawBytes(data.data(), data.size());
00167         file.close();
00168         if (setExecutable)
00169                 chmod(fileName, 0755);
00170         return true;
00171 }
00172 
00173 bool QGit::readFromFile(SCRef fileName, QString& data) {
00174 
00175         data = "";
00176         QFile file(fileName);
00177         if (!file.open(IO_ReadOnly)) {
00178                 dbp("ERROR: unable to read file %1", fileName);
00179                 return false;
00180         }
00181         QTextStream stream(&file);
00182         data = stream.read();
00183         file.close();
00184         return true;
00185 }
00186 
00187 void QGit::baAppend(QByteArray** baPtr, const char* ascii, int len) {
00188 
00189         QByteArray* ba = *baPtr;
00190         uint oldSize = 0;
00191         if (ba) {
00192                 oldSize = ba->size();
00193                 ba->resize(oldSize + len, QGArray::SpeedOptim);
00194         } else {
00195                 ba = new QByteArray(len);
00196                 *baPtr = ba;
00197         }
00198         // no detach here, no copy on write
00199         memcpy(ba->data() + oldSize, ascii, len);
00200 }
00201 
00202 void QGit::baAppend(QByteArray& ba, const QByteArray& src) {
00203 
00204         QByteArray* baPtr = &ba;
00205         baAppend(&baPtr, src.data(),  src.size());
00206 }

Generated on Fri Dec 7 21:57:38 2007 for QGit by  doxygen 1.5.3