File indexing completed on 2024-05-12 16:23:30

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2008 by Bjoern Erik Nilsen & Fredrik Berg Kjoelstad*
0003  *   bjoern.nilsen@bjoernen.com & fredrikbk@hotmail.com                    *
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 #include "nonguifrontend.h"
0021 
0022 #include "domainfacade.h"
0023 #include "frontends/frontend.h"
0024 #include "uiexception.h"
0025 #include "src/technical/stringiterator.h"
0026 
0027 #include <stdio.h>
0028 #include <getopt.h>
0029 #include <stdlib.h>
0030 #include <dirent.h>
0031 #include <sys/stat.h>
0032 #include <errno.h>
0033 #include <string>
0034 #include <string.h>
0035 #include <sstream>
0036 
0037 
0038 NonGUIFrontend::NonGUIFrontend(DomainFacade *facadePtr)
0039         : facadePtr(facadePtr) {
0040 }
0041 
0042 
0043 NonGUIFrontend::~ NonGUIFrontend() {
0044 }
0045 
0046 
0047 int NonGUIFrontend::run(int argc, char **argv) {
0048     printf("NonGUIFrontend starts running ...\n");
0049     return parseArguments(argc, argv);
0050 }
0051 
0052 
0053 void NonGUIFrontend::showProgress(ProgressMessage message,
0054         int /*numOperations*/) {
0055     // sets the stdout to be unbuffered
0056     setvbuf(stdout, NULL, _IONBF,0);
0057     const char* infoText = "Please wait";
0058     switch (message) {
0059     case connectingCamera:
0060         infoText = "Connecting camera";
0061         break;
0062     case exporting:
0063         infoText = "Exporting";
0064         break;
0065     case importingFramesFromDisk:
0066         infoText = "Importing frames from disk";
0067         break;
0068     case restoringProject:
0069         infoText = "Restoring project";
0070         break;
0071     case savingScenesToDisk:
0072         infoText = "Saving scenes to disk";
0073         break;
0074     }
0075     printf("%s...", infoText);
0076 }
0077 
0078 
0079 void NonGUIFrontend::hideProgress() {
0080     printf("\nOperation finished!\n");
0081     // sets the stdout the be buffered
0082     setvbuf (stdout, NULL , _IOFBF , 1024);
0083 }
0084 
0085 
0086 void NonGUIFrontend::updateProgress(int) {
0087     printf(".");
0088 }
0089 
0090 
0091 bool NonGUIFrontend::isOperationAborted() {
0092     return false;
0093 }
0094 
0095 void NonGUIFrontend::processEvents() {
0096 }
0097 
0098 int NonGUIFrontend::parseArguments(int argc, char **argv) {
0099     while (1) {
0100         static struct option longOptions[] =
0101             {
0102                 {"nonGUI", 0, 0, 0},
0103                 {"addFrames", 1, 0, 'a'},
0104                 {"capture", 1, 0, 'c'},
0105                 {"save", 1, 0, 's'},
0106                 {"export", 0, 0, 'e'},
0107                 {0, 0, 0, 0}
0108             };
0109 
0110         /* getopt_long  stores the option index here. */
0111         int optionIndex = 0;
0112         int c = getopt_long(argc, argv, "a:c:s:e:", longOptions, &optionIndex);
0113 
0114         /* Detect the end of the options. */
0115         if (c == -1) {
0116             break;
0117         }
0118 
0119         switch (c) {
0120             case 0:
0121                 /* If this option set a flag, do nothing else now. */
0122                 break;
0123 
0124             case 'a':
0125                 //printf ("option --addFrames with argument %s\n", optarg);
0126                 addFrames(optarg);
0127                 break;
0128 
0129             case 'c':
0130                 printf ("option --capture with argument %s\n", optarg);
0131                 break;
0132 
0133             case 'e':
0134                 printf ("option --export with argument %s\n", optarg);
0135                 break;
0136 
0137             case 's':
0138                 //printf ("option --save with argument %s\n", optarg);
0139                 save(optarg);
0140                 break;
0141 
0142             case '?':
0143                 /* getopt_long  already printed an error message. */
0144                 break;
0145 
0146             default:
0147                 return 1;
0148         }
0149     }
0150     return 0;
0151 }
0152 
0153 class DirIterator : public StringIterator {
0154     std::string buffer;
0155     std::string::size_type dirLen;
0156     DIR *dp;
0157     struct dirent *ep;
0158 public:
0159     DirIterator(const char* path)
0160             : buffer(path), dirLen(0), dp(opendir(path)) {
0161         dirLen = buffer.size();
0162         next();
0163     }
0164     ~DirIterator() {
0165         if (dp)
0166             closedir(dp);
0167     }
0168     int count() {
0169         int c = 0;
0170         if (dp) {
0171             while (atEnd()) {
0172                 ++c;
0173                 next();
0174             }
0175             rewinddir(dp);
0176             next();
0177         }
0178         return c;
0179     }
0180     bool atEnd() const {
0181         return !dp || !ep;
0182     }
0183     const char* get() const {
0184         return &buffer[0];
0185     }
0186     void next() {
0187         if (dp) {
0188             ep = readdir(dp);
0189             while (ep && ep->d_type != DT_REG)
0190                 ep = readdir(dp);
0191             if (ep) {
0192                 buffer.resize(dirLen);
0193                 buffer.append(ep->d_name);
0194                 buffer.c_str();
0195             }
0196         }
0197     }
0198 };
0199 
0200 void NonGUIFrontend::addFrames(const char *directory) {
0201     std::string dir;
0202     getAbsolutePath(dir, directory);
0203     DirIterator di(dir.c_str());
0204     facadePtr->addFrames(0, 0, di);
0205 }
0206 
0207 
0208 void NonGUIFrontend::save(const char *directory) {
0209     // returns a absolute path which is allocated with new[]
0210     std::string dir;
0211     getAbsolutePath(dir, directory);
0212     facadePtr->saveProject(dir.c_str());
0213 }
0214 
0215 
0216 void NonGUIFrontend::getAbsolutePath(std::string& out, const char *path) {
0217     if (!path || path[0] == '\0') {
0218         out = "/";
0219         return;
0220     }
0221     // isn't an absolute path
0222     if (path[0] != '/') {
0223         // make it absolute
0224         out = getenv("PWD");
0225         out.append("/");
0226         out.append(path);
0227     } else {
0228         out = path;
0229     }
0230     // ensure it ends in /
0231     if (out[out.size() - 1] != '/')
0232         out.append("/");
0233 }
0234 
0235 
0236 int NonGUIFrontend::checkFiles(const char *directory) {
0237     int numSuccessful = 0;
0238     DIR *dp = opendir(directory);
0239 
0240     if (dp) {
0241         struct dirent *ep;
0242         struct stat st;
0243         std::stringstream path;
0244 
0245         while ( (ep = readdir(dp)) ) {
0246             path.str("");
0247             path << directory << ep->d_name;
0248             std::string p(path.str());
0249             stat(p.c_str(), &st);
0250             // is a regular file, not a directory
0251             if ( S_ISREG(st.st_mode) != 0) {
0252                 ++numSuccessful;
0253             }
0254         }
0255         closedir(dp);
0256     }
0257     else {
0258         fprintf (stderr, "Couldn't open directory: %s; %s\n",directory, strerror (errno));
0259     }
0260 
0261     return numSuccessful;
0262 }
0263 
0264 
0265 void NonGUIFrontend::handleException(UiException& e) {
0266     printf("%s\n", e.what());
0267     if (e.warning() == UiException::IsError)
0268         throw CriticalError();
0269 }
0270 
0271 
0272 void NonGUIFrontend::reportWarning(const char *message) {
0273     printf("Warning: %s\n", message);
0274 }
0275 
0276 
0277 bool NonGUIFrontend::askQuestion(Question) {
0278     return false;
0279 }
0280 
0281 
0282 int NonGUIFrontend::runExternalCommand(const char *) {
0283     return 1;
0284 }