File indexing completed on 2024-05-05 13:19:24
0001 /*************************************************************************** 0002 * Copyright (C) 2014 by Linuxstopmotion contributors; * 0003 * see the AUTHORS file for details. * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include "externalcommandwithtemporarydirectory.h" 0022 #include "externalcommand.h" 0023 0024 #include <QString> 0025 0026 #include <src/technical/util.h> 0027 #include <unistd.h> 0028 #include <cstdlib> 0029 #include <cstring> 0030 #include <exception> 0031 #include <string> 0032 #include <memory> 0033 0034 class TemporaryDirectoryCreationException : public std::exception { 0035 char msg[100]; 0036 public: 0037 TemporaryDirectoryCreationException(const char* reason) { 0038 strncpy(msg, reason, sizeof(msg) - 1); 0039 msg[sizeof(msg) - 1] = '\0'; 0040 } 0041 const char* what() const throw() { 0042 return msg; 0043 } 0044 }; 0045 0046 class TemporaryDirectory { 0047 std::string path; 0048 const char* pptr; 0049 bool tryTemplate(const char* var, const char* baseDir) { 0050 path = ""; 0051 if (var) { 0052 const char* v = getenv(var); 0053 if (!v) 0054 return false; 0055 path += v; 0056 } 0057 if (baseDir) 0058 path += baseDir; 0059 path += "/lsmXXXXXX"; 0060 pptr = path.c_str(); 0061 return mkdtemp(&path[0]); 0062 } 0063 public: 0064 //TODO there's got to be a better way of making a temporary directory 0065 TemporaryDirectory() : pptr(0) { 0066 if (!tryTemplate("TMPDIR", 0) 0067 && !tryTemplate(0, "/tmp") 0068 && !tryTemplate("HOME", "/.stopmotion")) { 0069 throw TemporaryDirectoryCreationException( 0070 "Failed to create temporary directory"); 0071 } 0072 } 0073 ~TemporaryDirectory() { 0074 Util::removeDirectoryContents(pptr); 0075 rmdir(pptr); 0076 } 0077 const char* getPath() const { 0078 return pptr; 0079 } 0080 }; 0081 0082 ExternalCommandWithTemporaryDirectory 0083 ::ExternalCommandWithTemporaryDirectory( 0084 QWidget* parent) :ec(0), td(0) { 0085 std::unique_ptr<TemporaryDirectory> ttd(new TemporaryDirectory); 0086 ec = new ExternalCommand(parent); 0087 td = ttd.release(); 0088 } 0089 0090 ExternalCommandWithTemporaryDirectory 0091 ::~ExternalCommandWithTemporaryDirectory() { 0092 delete ec; 0093 ec = 0; 0094 delete td; 0095 td = 0; 0096 } 0097 0098 void ExternalCommandWithTemporaryDirectory::run(const QString& command) { 0099 ec->run(command); 0100 } 0101 0102 void ExternalCommandWithTemporaryDirectory::show() { 0103 ec->show(); 0104 } 0105 0106 const char* ExternalCommandWithTemporaryDirectory 0107 ::getTemporaryDirectoryPath() const { 0108 return td->getPath(); 0109 }