36 #include <QTextStream> 42 #include <QApplication> 56 mActivePatientFolder = this->getNoPatientFolder();
59 QTimer::singleShot(100,
this, SLOT(startupLoadPatient()));
74 bool valid = this->isValidSessionFolder(dir);
75 bool exists = this->folderExists(dir);
79 this->loadSession(dir);
83 this->initializeNewSession(dir);
87 reportError(QString(
"Failed to initialize session in existing folder with no valid session: %1").arg(dir));
91 QString SessionStorageServiceImpl::getNoPatientFolder()
const 97 bool SessionStorageServiceImpl::isValidSessionFolder(QString dir)
const 99 QString filename = QDir(dir).absoluteFilePath(this->getXmlFileName());
100 return QFileInfo::exists(filename);
103 bool SessionStorageServiceImpl::folderExists(QString dir)
const 105 return QFileInfo(dir).exists() && QFileInfo(dir).isDir();
108 void SessionStorageServiceImpl::loadSession(QString dir)
110 if (this->isActivePatient(dir))
112 this->loadPatientSilent(dir);
113 this->reportActivePatient();
114 this->writeRecentPatientData();
117 void SessionStorageServiceImpl::initializeNewSession(QString dir)
119 this->clearPatientSilent();
120 dir = this->convertToValidFolderName(dir);
121 this->createPatientFolders(dir);
122 this->setActivePatient(dir);
124 this->reportActivePatient();
125 this->writeRecentPatientData();
128 QString SessionStorageServiceImpl::getXmlFileName()
const 130 return "custusdoc.xml";
140 this->generateSaveDoc(doc.
doc());
142 QDomElement element = doc.
doc().documentElement();
145 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
147 report(
"Saved patient " + mActivePatientFolder);
152 this->clearPatientSilent();
153 this->reportActivePatient();
154 this->writeRecentPatientData();
159 return !mActivePatientFolder.isEmpty() &&
160 (mActivePatientFolder != this->getNoPatientFolder());
165 return mActivePatientFolder;
168 void SessionStorageServiceImpl::reportActivePatient()
170 report(
"Set Active Patient: " + mActivePatientFolder);
173 void SessionStorageServiceImpl::setActivePatient(
const QString& activePatientFolder)
175 if (activePatientFolder == mActivePatientFolder)
178 mActivePatientFolder = activePatientFolder;
183 void SessionStorageServiceImpl::onSessionChanged()
188 void SessionStorageServiceImpl::clearPatientSilent()
190 this->setActivePatient(this->getNoPatientFolder());
194 bool SessionStorageServiceImpl::isActivePatient(QString patient)
const 196 return (patient == mActivePatientFolder);
199 void SessionStorageServiceImpl::loadPatientSilent(QString choosenDir)
201 if (this->isActivePatient(choosenDir))
203 this->clearPatientSilent();
204 if (choosenDir == QString::null)
207 QString filename = QDir(choosenDir).absoluteFilePath(this->getXmlFileName());
209 mActivePatientFolder = choosenDir;
214 QDomElement element = doc.documentElement();
225 void SessionStorageServiceImpl::writeRecentPatientData()
231 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
233 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
235 QDomElement patientNode = doc.createElement(
"patient");
238 QDomElement versionName = doc.createElement(
"version_name");
239 versionName.appendChild(doc.createTextNode(this->getVersionName()));
240 patientNode.appendChild(versionName);
242 QDomElement activePatientNode = doc.createElement(
"active_patient");
243 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
244 patientNode.appendChild(activePatientNode);
245 doc.appendChild(patientNode);
248 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const 250 if (!dir.endsWith(
".cx3"))
255 void SessionStorageServiceImpl::createPatientFolders(QString dir)
258 QDir().mkpath(dir+
"/Images");
259 QDir().mkpath(dir+
"/Logs");
260 QDir().mkpath(dir+
"/US_Acq");
263 QString SessionStorageServiceImpl::getVersionName()
265 return QString(
"%1").arg(CustusX_VERSION_STRING);
268 void SessionStorageServiceImpl::clearCache()
278 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
280 int doLoad = QApplication::arguments().indexOf(
"--load");
283 if (doLoad + 1 >= QApplication::arguments().size())
286 QString folder = QApplication::arguments()[doLoad + 1];
293 void SessionStorageServiceImpl::startupLoadPatient()
295 QString folder = this->getCommandLineStartupPatient();
297 if (!folder.isEmpty())
299 report(QString(
"Startup Load [%1] from command line").arg(folder));
302 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
304 folder =
settings()->
value(
"startup/lastPatient").toString();
305 if(this->isValidSessionFolder(folder))
308 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
309 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
310 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
311 if (minsSinceLastSave > allowedMinsSinceLastSave)
314 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
315 .arg(
int(minsSinceLastSave/60))
316 .arg(
int(allowedMinsSinceLastSave/60)));
319 if (!folder.isEmpty())
320 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
324 report(
"Startup Load: Ignored recent patient because it is not valid anymore");
330 if (folder.isEmpty())
virtual bool isNull() const
static void writeXmlFile(QDomDocument &doc, QString &filename)
void reportError(QString msg)
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value...
std::string toString(T const &value)
converts any type to a string
bool removeNonemptyDirRecursively(const QString &dirName)
void isLoadingSecond(QDomElement &root)
Emitted after the isLoading signal, to allow for structures that must be loaded after core structures...
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
static QDomDocument readXmlFile(QString &filename)
SessionStorageServiceImpl(ctkPluginContext *context)
void isSaving(QDomElement &root)
xml storage is available
virtual QString getRootFolder() const
Namespace for all CustusX production code.