ProfileSelect::ProfileSelect(QWidget *parent) : QDialog(parent), ui(new Ui::ProfileSelect) { ui->setupUi(this); QStringList str; model=new QStandardItemModel (0,0); int i=0; int sel=-1; QString name; QIcon icon(":/icons/moon.png"); for (QMap<QString,Profile *>::iterator p=Profiles::profiles.begin();p!=Profiles::profiles.end();p++) { name=p.key(); QStandardItem *item=new QStandardItem(icon,name); if (PREF.contains(STR_GEN_Profile) && (name==PREF[STR_GEN_Profile].toString())) { sel=i; } item->setData(p.key()); item->setEditable(false); // Profile fonts arern't loaded yet.. Using generic font. item->setFont(QFont("Sans Serif",18,QFont::Bold,false)); model->appendRow(item); i++; } ui->listView->setModel(model); ui->listView->setSelectionBehavior(QAbstractItemView::SelectRows); ui->listView->setSelectionMode(QAbstractItemView::SingleSelection); if (sel>=0) ui->listView->setCurrentIndex(model->item(sel)->index()); m_tries=0; /*PREF["SkipLogin"]=false; if ((i==1) && PREF["SkipLogin"].toBool()) { if (!Profiles::profiles.contains(name)) PREF[STR_GEN_Profile]=name; QTimer::singleShot(0,this,SLOT(earlyExit())); hide(); } */ popupMenu=new QMenu(this); popupMenu->addAction(tr("Open Profile"),this,SLOT(openProfile())); popupMenu->addAction(tr("Edit Profile"),this,SLOT(editProfile())); popupMenu->addSeparator(); popupMenu->addAction(tr("Delete Profile"),this,SLOT(deleteProfile())); ui->labelAppName->setText(STR_TR_SleepyHead); ui->labelVersion->setText("v"+VersionString+" "+ReleaseStatus); // if (GIT_BRANCH!="master") // ui->labelBuild->setText(GIT_BRANCH); // else ui->labelBuild->setText(QString()); ui->labelFolder->setText(GetAppRoot()); ui->labelFolder->setToolTip("Current SleepyHead data folder\n"+GetAppRoot()); }
Preferences::Preferences(QString name,QString filename) { const QString xmlext=".xml"; if (name.endsWith(xmlext)) { p_name=name.section(".",0,0); } else { p_name=name; } if (filename.isEmpty()) { p_filename=GetAppRoot()+"/"+p_name+xmlext; } else { if (!filename.contains("/")) { p_filename=GetAppRoot()+"/"; } else p_filename=""; p_filename+=filename; if (!p_filename.endsWith(xmlext)) p_filename+=xmlext; } }
const QString Preferences::Get(QString name) { QString temp; QChar obr=QChar('{'); QChar cbr=QChar('}'); QString t,a,ref; // How I miss Regular Expressions here.. if (p_preferences.find(name)!=p_preferences.end()) { temp=""; t=p_preferences[name].toString(); if (p_preferences[name].type()!=QVariant::String) { return t; } } else { t=name; // parse the string.. } while (t.contains(obr)) { temp+=t.section(obr,0,0); a=t.section(obr,1); if (a.startsWith("{")) { temp+=obr; t=a.section(obr,1); continue; } ref=a.section(cbr,0,0); if (ref.toLower()=="home") { temp+=GetAppRoot(); } else if (ref.toLower()=="user") { temp+=getUserName(); } else if (ref.toLower()=="sep") { // redundant in QT temp+=QDir::separator(); } else { temp+=Get(ref); } t=a.section(cbr,1); } temp+=t; temp.replace("}}","}"); // Make things look a bit better when escaping braces. return temp; }
void ProfileSelect::deleteProfile() { QString name=ui->listView->currentIndex().data().toString(); if (QMessageBox::question(this,tr("Question"),tr("Are you sure you want to trash the profile \"%1\"?").arg(name),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes){ if (QMessageBox::question(this,tr("Question"),tr("Double Checking:\n\nDo you really want \"%1\" profile to be obliterated?").arg(name),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes){ if (QMessageBox::question(this,tr("Question"),tr("Okay, I am about to totally OBLITERATE the profile \"%1\" and all it's contained data..\n\nDon't say you weren't warned. :-p").arg(name),QMessageBox::Cancel,QMessageBox::Ok)==QMessageBox::Ok){ bool reallydelete=false; Profile *profile=Profiles::profiles[name]; if (!profile) { QMessageBox::warning(this,tr("WTH???"),tr("If you can read this you need to delete this profile directory manually (It's under %1)").arg(GetAppRoot()+"/Profiles/"+PROFILE.user->userName()),QMessageBox::Ok); return; } if (profile->user->hasPassword()) { QDialog dialog(this,Qt::Dialog); QLineEdit *e=new QLineEdit(&dialog); e->setEchoMode(QLineEdit::Password); dialog.connect(e,SIGNAL(returnPressed()),&dialog,SLOT(accept())); dialog.setWindowTitle(tr("Enter Password for %1").arg(name)); dialog.setMinimumWidth(300); QVBoxLayout *lay=new QVBoxLayout(); dialog.setLayout(lay); lay->addWidget(e); int tries=0; do { e->setText(""); if (dialog.exec()!=QDialog::Accepted) break; tries++; if (profile->user->checkPassword(e->text())) { reallydelete=true; break; } else { if (tries<3) { QMessageBox::warning(this,STR_MESSAGE_ERROR,tr("Incorrect Password"),QMessageBox::Ok); } else { QMessageBox::warning(this,STR_MESSAGE_ERROR,tr("Meheh... If your trying to delete because you forgot the password, your going the wrong way about it. Read the docs.\n\nSigned: Nasty Programmer"),QMessageBox::Ok); } } } while (tries<3); } else reallydelete=true; if (reallydelete) { QString path=profile->Get(PrefMacro(STR_GEN_DataFolder)); if (!path.isEmpty()) { if (!removeDir(path)) { QMessageBox::information(this,tr("Whoops."),tr("There was an error deleting the profile directory.. You need to manually remove %1").arg(path),QMessageBox::Ok); } } model->removeRow(ui->listView->currentIndex().row()); qDebug() << "Delete" << path; } } } } }
Preferences::Preferences() { p_name="Preferences"; p_path=GetAppRoot(); }