File indexing completed on 2025-01-19 04:22:44
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "gitfile.h" 0008 #include "gitmanager.h" 0009 0010 #include <QFile> 0011 #include <QFileInfo> 0012 #include <QStandardPaths> 0013 #include <QUuid> 0014 #include <utility> 0015 0016 namespace Git 0017 { 0018 0019 File::StorageType File::storage() const 0020 { 0021 return mStorage; 0022 } 0023 0024 File::File() 0025 : mStorage{InValid} 0026 { 0027 } 0028 0029 File::File(QString filePath) 0030 : mPlace() 0031 , mFilePath(std::move(filePath)) 0032 , mStorage{Local} 0033 { 0034 } 0035 0036 File::File(Manager *git, QString place, QString filePath) 0037 : mPlace(std::move(place)) 0038 , mFilePath(std::move(filePath)) 0039 , mGit(git) 0040 , mStorage{Git} 0041 { 0042 Q_ASSERT(mGit); 0043 } 0044 0045 File::File(const File &other) = default; 0046 0047 // File::File(File &&other) 0048 // : _place(std::move(other._place)), _filePath(std::move(other._filePath)), 0049 // _git(std::move(other._git)), _storage(std::move(other._storage)) 0050 //{ 0051 0052 //} 0053 0054 File &File::operator=(const File &other) = default; 0055 0056 const QString &File::place() const 0057 { 0058 return mPlace; 0059 } 0060 0061 void File::setPlace(const QString &newPlace) 0062 { 0063 mPlace = newPlace; 0064 } 0065 0066 const QString &File::fileName() const 0067 { 0068 return mFilePath; 0069 } 0070 0071 void File::setFileName(const QString &newFileName) 0072 { 0073 mFilePath = newFileName; 0074 } 0075 0076 Manager *File::git() const 0077 { 0078 return mGit; 0079 } 0080 0081 void File::setGit(Manager *newGit) 0082 { 0083 mGit = newGit; 0084 } 0085 0086 QString File::displayName() const 0087 { 0088 switch (mStorage) { 0089 case InValid: 0090 return {}; 0091 case Local: 0092 return mFilePath; 0093 case Git: 0094 return mPlace + QLatin1Char(':') + mFilePath; 0095 } 0096 0097 return {}; 0098 } 0099 0100 bool File::save(const QString &path) const 0101 { 0102 QFile f{path}; 0103 if (!f.open(QIODevice::WriteOnly)) 0104 return false; 0105 const auto buffer = mGit->runGit({QStringLiteral("show"), mPlace + QLatin1Char(':') + mFilePath}); 0106 f.write(buffer); 0107 f.close(); 0108 return true; 0109 } 0110 0111 QString File::saveAsTemp() const 0112 { 0113 QFileInfo fi{mFilePath}; 0114 QString fileName; 0115 const auto tempLocation = QStandardPaths::writableLocation(QStandardPaths::TempLocation); 0116 if (tempLocation.isEmpty()) 0117 return {}; 0118 0119 fileName = QStringLiteral("%1/%2-%3.%4").arg(tempLocation, fi.baseName(), QUuid::createUuid().toString(QUuid::Id128), fi.suffix()); 0120 0121 if (save(fileName)) 0122 return fileName; 0123 return {}; 0124 } 0125 0126 QString File::content() const 0127 { 0128 switch (mStorage) { 0129 case InValid: 0130 return {}; 0131 case Local: { 0132 QFile f(mFilePath); 0133 if (!f.open(QIODevice::ReadOnly)) 0134 return {}; 0135 return f.readAll(); 0136 } 0137 case Git: 0138 return mGit->runGit({QStringLiteral("show"), mPlace + QLatin1Char(':') + mFilePath}); 0139 } 0140 0141 return {}; 0142 } 0143 0144 } // namespace Git