36 #include <QTextStream>
42 #include <QApplication>
53 mActivePatientFolder = this->getNoPatientFolder();
56 QTimer::singleShot(100,
this, SLOT(startupLoadPatient()));
71 bool valid = this->isValidSessionFolder(dir);
72 bool exists = this->folderExists(dir);
76 this->loadSession(dir);
80 this->initializeNewSession(dir);
84 reportError(QString(
"Failed to initialize session in existing folder with no valid session: %1").arg(dir));
88 QString SessionStorageServiceImpl::getNoPatientFolder()
const
94 bool SessionStorageServiceImpl::isValidSessionFolder(QString dir)
const
96 QString filename = QDir(dir).absoluteFilePath(this->getXmlFileName());
97 return QFileInfo::exists(filename);
100 bool SessionStorageServiceImpl::folderExists(QString dir)
const
102 return QFileInfo(dir).exists() && QFileInfo(dir).isDir();
105 void SessionStorageServiceImpl::loadSession(QString dir)
107 if (this->isActivePatient(dir))
109 this->loadPatientSilent(dir);
110 this->reportActivePatient();
111 this->writeRecentPatientData();
114 void SessionStorageServiceImpl::initializeNewSession(QString dir)
116 this->clearPatientSilent();
117 dir = this->convertToValidFolderName(dir);
118 this->createPatientFolders(dir);
119 this->setActivePatient(dir);
121 this->reportActivePatient();
122 this->writeRecentPatientData();
125 QString SessionStorageServiceImpl::getXmlFileName()
const
127 return "custusdoc.xml";
139 this->generateSaveDoc(doc);
141 QDomElement element = doc.documentElement();
144 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
145 this->writeXmlFile(doc, filename);
147 report(
"Saved patient " + mActivePatientFolder);
151 void SessionStorageServiceImpl::writeXmlFile(QDomDocument doc, QString filename)
153 QFile file(filename);
154 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
156 QTextStream stream(&file);
157 stream << doc.toString(4);
162 reportError(
"Could not open " + file.fileName() +
" Error: " + file.errorString());
168 this->clearPatientSilent();
169 this->reportActivePatient();
170 this->writeRecentPatientData();
175 return !mActivePatientFolder.isEmpty() &&
176 (mActivePatientFolder != this->getNoPatientFolder());
181 return mActivePatientFolder;
184 void SessionStorageServiceImpl::reportActivePatient()
186 report(
"Set Active Patient: " + mActivePatientFolder);
189 void SessionStorageServiceImpl::setActivePatient(
const QString& activePatientFolder)
191 if (activePatientFolder == mActivePatientFolder)
194 mActivePatientFolder = activePatientFolder;
199 void SessionStorageServiceImpl::onSessionChanged()
204 void SessionStorageServiceImpl::clearPatientSilent()
206 this->setActivePatient(this->getNoPatientFolder());
210 bool SessionStorageServiceImpl::isActivePatient(QString patient)
const
212 return (patient == mActivePatientFolder);
215 void SessionStorageServiceImpl::loadPatientSilent(QString choosenDir)
217 if (this->isActivePatient(choosenDir))
219 this->clearPatientSilent();
220 if (choosenDir == QString::null)
223 QString filename = QDir(choosenDir).absoluteFilePath(this->getXmlFileName());
224 QDomDocument doc = this->readXmlFile(filename);
226 mActivePatientFolder = choosenDir;
231 QDomElement element = doc.documentElement();
238 QDomDocument SessionStorageServiceImpl::readXmlFile(QString filename)
241 QFile file(filename);
242 if (!file.open(QIODevice::ReadOnly))
244 reportError(
"Could not open XML file :" + file.fileName() +
".");
245 return QDomDocument();
251 if (!retval.setContent(&file,
false, &emsg, &eline, &ecolumn))
253 reportError(
"Could not parse XML file :" + file.fileName() +
" because: " + emsg +
"");
254 return QDomDocument();
263 void SessionStorageServiceImpl::writeRecentPatientData()
269 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
271 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
273 QDomElement patientNode = doc.createElement(
"patient");
276 QDomElement versionName = doc.createElement(
"version_name");
277 versionName.appendChild(doc.createTextNode(this->getVersionName()));
278 patientNode.appendChild(versionName);
280 QDomElement activePatientNode = doc.createElement(
"active_patient");
281 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
282 patientNode.appendChild(activePatientNode);
283 doc.appendChild(patientNode);
286 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const
288 if (!dir.endsWith(
".cx3"))
293 void SessionStorageServiceImpl::createPatientFolders(QString dir)
296 QDir().mkpath(dir+
"/Images");
297 QDir().mkpath(dir+
"/Logs");
298 QDir().mkpath(dir+
"/US_Acq");
301 QString SessionStorageServiceImpl::getVersionName()
303 return QString(
"%1").arg(CustusX_VERSION_STRING);
306 void SessionStorageServiceImpl::clearCache()
316 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
318 int doLoad = QApplication::arguments().indexOf(
"--load");
321 if (doLoad + 1 >= QApplication::arguments().size())
324 QString folder = QApplication::arguments()[doLoad + 1];
331 void SessionStorageServiceImpl::startupLoadPatient()
333 QString folder = this->getCommandLineStartupPatient();
335 if (!folder.isEmpty())
337 report(QString(
"Startup Load [%1] from command line").arg(folder));
340 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
342 folder =
settings()->
value(
"startup/lastPatient").toString();
345 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
346 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
347 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
348 if (minsSinceLastSave > allowedMinsSinceLastSave)
351 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
352 .arg(
int(minsSinceLastSave/60))
353 .arg(
int(allowedMinsSinceLastSave/60)));
357 if (!folder.isEmpty())
358 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
361 if (folder.isEmpty())
virtual bool isNull() const
void reportError(QString msg)
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value...
bool removeNonemptyDirRecursively(const QString &dirName)
virtual void save()
Save all application data to XML file.
virtual void load(QString dir)
load session from dir, or create new session in this location if none exist
virtual bool isValid() const
virtual ~SessionStorageServiceImpl()
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
QString timestampSecondsFormat()
void sessionChanged()
emitted after change to a new session (new or loaded or cleared)
void setValue(const QString &key, const QVariant &value)
static QString getCachePath()
return path to a folder that is used during execution, will be cleared at start and stop...
QString getSubFolder(QString relative)
return and create a folder/path relative to root. Created if necessary.
Settings * settings()
Shortcut for accessing the settings instance.
void cleared()
emitted when session is cleared, before isLoading is called
SessionStorageServiceImpl(ctkPluginContext *context)
void isSaving(QDomElement &root)
xml storage is available
virtual QString getRootFolder() const