1 /* 2 * Copyright (c) 2014-2020 Meltytech, LLC 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "qmlfile.h" 19 #include <QFileInfo> 20 #include <QFile> 21 #include <QDir> 22 #include <Logger.h> 23 QmlFile(QObject * parent)24QmlFile::QmlFile(QObject* parent) 25 : QObject(parent) 26 , m_url() 27 { 28 } 29 getUrl()30QString QmlFile::getUrl() 31 { 32 return m_url.toString(); 33 } 34 setUrl(QString text)35void QmlFile::setUrl(QString text) 36 { 37 QUrl url = text.replace('\\', "/"); 38 QUrl::FormattingOptions options = 39 QUrl::RemoveScheme | 40 QUrl::RemovePassword | 41 QUrl::RemoveUserInfo | 42 QUrl::RemovePort | 43 QUrl::RemoveAuthority | 44 QUrl::RemoveQuery; 45 #ifdef Q_OS_WIN 46 // If the scheme is a drive letter, do not remove it. 47 if (url.scheme().size() == 1) { 48 options ^= QUrl::RemoveScheme; 49 // QUrl removes the host from a UNC path when removing the scheme. 50 } else if (text.startsWith("file://") && text.size() > 9 && text[9] != ':') { 51 options ^= QUrl::RemoveScheme; 52 options ^= QUrl::RemoveAuthority; 53 } 54 55 QUrl adj = url.adjusted(options); 56 QString s = adj.toString(); 57 58 // If there is a slash before a drive letter. 59 // On Windows, file URLs look like file:///C:/Users/.... 60 // The scheme is removed but only "://" (not 3 slashes) between scheme and path. 61 if (s.size() > 2 && s[0] == '/' && s[2] == ':') { 62 // Remove the leading slash. 63 adj = s.mid(1); 64 } else if (s.startsWith("file://")) { // UNC path 65 // Remove the scheme. 66 adj = s.mid(5); 67 } 68 #else 69 QUrl adj = url.adjusted(options); 70 #endif 71 72 if(m_url != adj) { 73 m_url = adj; 74 emit urlChanged(m_url); 75 } 76 } 77 getFileName()78QString QmlFile::getFileName() 79 { 80 return QFileInfo(m_url.toString()).fileName(); 81 } 82 getPath()83QString QmlFile::getPath() 84 { 85 return QDir::toNativeSeparators(QFileInfo(m_url.toString()).path()); 86 } 87 getFilePath()88QString QmlFile::getFilePath() 89 { 90 return QDir::toNativeSeparators(m_url.toString()); 91 } 92 copyFromFile(QString source)93void QmlFile::copyFromFile(QString source) 94 { 95 if (QFile::exists(m_url.toString())) 96 { 97 QFile::remove(m_url.toString()); 98 } 99 100 QFile inFile(source); 101 QFile outfile(m_url.toString()); 102 inFile.open(QFile::ReadOnly); 103 outfile.open(QFile::WriteOnly); 104 outfile.write(inFile.readAll()); 105 outfile.close(); 106 } 107 exists()108bool QmlFile::exists() 109 { 110 return QFileInfo(m_url.toString()).exists(); 111 } 112 suffix()113QString QmlFile::suffix() 114 { 115 return QFileInfo(m_url.toString()).suffix(); 116 } 117