NCL Composer  0.1.5
 All Classes Functions Variables Pages
Utilities.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "util/Utilities.h"
11 
12 #include <QStringList>
13 #include <QDebug>
14 
15 QMap<QString,LanguageType> createMap() {
16  QMap<QString,LanguageType> types;
17  types["cpr"] = NCL;
18  types["ncl"] = NCL;
19  types["smil"] = SMIL;
20  types["html"] = HTML;
21  return types;
22 }
23 
24 namespace composer {
25  namespace core {
26  namespace util {
27 
28 QMap<QString,LanguageType> Utilities::types = createMap();
29 
30 LanguageType Utilities::getLanguageTypeByExtension(QString ext)
31 {
32  if (!types.contains(ext)) return NONE;
33  else return types[ext];
34 }
35 
36 QString Utilities::getExtensionForLanguageType(LanguageType type)
37 {
38  QMap<QString,LanguageType>::iterator it;
39  for (it = types.begin(); it != types.end(); it++)
40  if(type == it.value())
41  return it.key();
42  return "";
43 }
44 
45 QString Utilities::relativePath( QString absolutePath, QString relativeTo,
46  bool bIsFile /*= false*/ )
47 {
48  //force the "/" instead of "\\"
49  absolutePath = absolutePath.replace("\\", "/");
50  relativeTo = relativeTo.replace("\\", "/");
51 
52  QStringList absoluteDirectories = absolutePath.split("/",
53  QString::SkipEmptyParts);
54  QStringList relativeDirectories = relativeTo.split("/",
55  QString::SkipEmptyParts);
56 
57  //Get the shortest of the two paths
58  int length =
59  absoluteDirectories.count() < relativeDirectories.count() ?
60  absoluteDirectories.count() : relativeDirectories.count();
61 
62  //Use to determine where in the loop we exited
63  int lastCommonRoot = -1;
64  int index;
65 
66  //Find common root
67  for (index = 0; index < length; index++)
68  if (absoluteDirectories[index] == relativeDirectories[index])
69  lastCommonRoot = index;
70  else
71  break;
72 
73  //If we didn't find a common prefix then throw
74  if (lastCommonRoot == -1)
75  throw QString("Paths do not have a common base");
76 
77  //Build up the relative path
78  QString relativePath;
79 
80  //Add on the ..
81  for (index = lastCommonRoot + 1;
82  index < absoluteDirectories.count() - (bIsFile?1:0); index++)
83  {
84  if (absoluteDirectories[index].length() > 0)
85  {
86  relativePath.append("../");
87 // relativePath.append(QDir::separator());
88  }
89  }
90 
91  //Add on the folders
92  for (index = lastCommonRoot + 1; index < relativeDirectories.count() - 1;
93  index++)
94  {
95  relativePath.append( relativeDirectories[index] ).append("/");
96  }
97 
98  relativePath.append(relativeDirectories[relativeDirectories.count() - 1]);
99 
100  return relativePath;
101 }
102 
103 QString Utilities::getLastFileDialogPath()
104 {
105  GlobalSettings settings;
106  QString lastFileDialogPath = QDir::homePath();
107 
108  settings.beginGroup("mainwindow"); // TODO: A better name to mainwindow.
109  if(settings.contains("lastFileDialogPath"))
110  lastFileDialogPath = settings.value("lastFileDialogPath").toString();
111  settings.endGroup();
112 
113  qDebug() << lastFileDialogPath;
114 
115  return lastFileDialogPath;
116 }
117 
118 void Utilities::updateLastFileDialogPath(QString filepath)
119 {
120  QFileInfo fileInfo(filepath);
121 
122  GlobalSettings settings;
123  settings.beginGroup("mainwindow");
124  settings.setValue("lastFileDialogPath", fileInfo.absolutePath());
125  settings.endGroup();
126 }
127 
128 } } } //end namespace
129