| 2991 | /** |
| 2992 | * \brief Try to prevent silent, automatic database upgrades, ask the user. |
| 2993 | * \returns -1 to use the existing schema, 0 to exit, 1 to upgrade. |
| 2994 | */ |
| 2995 | int MythContext::PromptForSchemaUpgrade(const QString &dbver, |
| 2996 | const QString ¤t) |
| 2997 | { |
| 2998 | bool autoUpgrade = false; |
| 2999 | bool connections = false; // Are (other) FE/BEs connected? |
| 3000 | bool expertMode = false; // Use existing schema? Multiple buttons? |
| 3001 | QString message; |
| 3002 | int returnValue = MYTH_SCHEMA_UPGRADE; |
| 3003 | bool upgradable = (dbver.toUInt() < current.toUInt()); |
| 3004 | QString warnOtherCl = tr("There are also other clients using this" |
| 3005 | " database. They should be shut down first."); |
| 3006 | |
| 3007 | |
| 3008 | // Users and developers can choose to live dangerously, |
| 3009 | // either to silently and automatically upgrade, |
| 3010 | // or an expert option to allow use of existing: |
| 3011 | switch (gContext->GetNumSetting("DBSchemaAutoUpgrade")) |
| 3012 | { |
| 3013 | case 1: autoUpgrade = true; break; |
| 3014 | case -1: expertMode = true; break; |
| 3015 | default: break; |
| 3016 | } |
| 3017 | |
| 3018 | |
| 3019 | // FIXME: Don't know how to determine this |
| 3020 | //if (getActiveConnections() > 1) |
| 3021 | // connections = true; |
| 3022 | |
| 3023 | |
| 3024 | // Deal with the trivial case first (No user prompting required) |
| 3025 | if (autoUpgrade && upgradable && !connections) |
| 3026 | return MYTH_SCHEMA_UPGRADE; |
| 3027 | |
| 3028 | |
| 3029 | // Build up strings used both in GUI and command shell contexts: |
| 3030 | if (upgradable) |
| 3031 | { |
| 3032 | if (autoUpgrade && connections) |
| 3033 | { |
| 3034 | message = tr("Error: MythTV cannot upgrade the schema of this" |
| 3035 | " datatase because other clients are using it.\n\n" |
| 3036 | "Please shut them down before upgrading."); |
| 3037 | returnValue = MYTH_SCHEMA_ERROR; |
| 3038 | } |
| 3039 | else |
| 3040 | { |
| 3041 | message = tr("Warning: MythTV wants to upgrade" |
| 3042 | " your database schema, from %1 to %2."); |
| 3043 | if (expertMode) |
| 3044 | message += "\n\n" + |
| 3045 | tr("You can try using the old schema," |
| 3046 | " but that may cause problems."); |
| 3047 | } |
| 3048 | } |
| 3049 | else // This client is too old |
| 3050 | { |
| 3051 | if (expertMode) |
| 3052 | message = tr("Warning: MythTV database has newer" |
| 3053 | " schema (%1) than expected (%2)."); |
| 3054 | else |
| 3055 | { |
| 3056 | message = tr("Error: MythTV database has newer" |
| 3057 | " schema (%1) than expected (%2)."); |
| 3058 | returnValue = MYTH_SCHEMA_ERROR; |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | if (message.contains("%1")) |
| 3063 | message = message.arg(dbver).arg(current); |
| 3064 | |
| 3065 | |
| 3066 | if (d->m_gui) |
| 3067 | { |
| 3068 | bool createdTempWindow = false; |
| 3069 | |
| 3070 | if (!d->mainWindow) |
| 3071 | { |
| 3072 | d->TempMainWindow(); |
| 3073 | createdTempWindow = true; |
| 3074 | } |
| 3075 | |
| 3076 | if (returnValue == MYTH_SCHEMA_ERROR) |
| 3077 | MythPopupBox::showExitPopup(d->mainWindow, |
| 3078 | "Database Upgrade Error", message); |
| 3079 | else |
| 3080 | { |
| 3081 | QStringList buttonNames; |
| 3082 | int selected; |
| 3083 | |
| 3084 | buttonNames += QObject::tr("Exit"); |
| 3085 | buttonNames += QObject::tr("Upgrade"); |
| 3086 | if (expertMode) |
| 3087 | buttonNames += QObject::tr("Use current schema"); |
| 3088 | |
| 3089 | selected = MythPopupBox::showButtonPopup(d->mainWindow, |
| 3090 | "Database Upgrade", |
| 3091 | message, buttonNames, -1); |
| 3092 | // The annoying extra confirmation: |
| 3093 | if (selected == 1) |
| 3094 | { |
| 3095 | message = tr("This cannot be un-done, so having a" |
| 3096 | " database backup would be a good idea."); |
| 3097 | if (connections) |
| 3098 | message += "\n\n" + warnOtherCl; |
| 3099 | |
| 3100 | selected = MythPopupBox::showButtonPopup(d->mainWindow, |
| 3101 | "Database Upgrade", |
| 3102 | message, |
| 3103 | buttonNames, -1); |
| 3104 | } |
| 3105 | |
| 3106 | switch (selected) |
| 3107 | { |
| 3108 | case 0: returnValue = MYTH_SCHEMA_EXIT; break; |
| 3109 | case 1: returnValue = MYTH_SCHEMA_UPGRADE; break; |
| 3110 | case 2: returnValue = MYTH_SCHEMA_USE_EXISTING; break; |
| 3111 | default: returnValue = MYTH_SCHEMA_ERROR; |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | if (createdTempWindow) |
| 3116 | d->EndTempWindow(); |
| 3117 | |
| 3118 | return returnValue; |
| 3119 | } |
| 3120 | |
| 3121 | QString resp; |
| 3122 | |
| 3123 | cout << endl << message << endl << endl; |
| 3124 | |
| 3125 | if (expertMode) |
| 3126 | { |
| 3127 | resp = d->getResponse("Would you like to use the existing schema?", |
| 3128 | "yes"); |
| 3129 | if (!resp || resp.left(1).lower() == "y") |
| 3130 | return MYTH_SCHEMA_USE_EXISTING; |
| 3131 | } |
| 3132 | |
| 3133 | resp = d->getResponse("\nShall I upgrade this database?", "yes"); |
| 3134 | if (resp && resp.left(1).lower() != "y") |
| 3135 | return MYTH_SCHEMA_EXIT; |
| 3136 | |
| 3137 | if (connections) |
| 3138 | cout << endl << warnOtherCl <<endl; |
| 3139 | |
| 3140 | resp = d->getResponse("\nA database backup might be a good idea" |
| 3141 | "\nAre you sure you want to upgrade?", "no"); |
| 3142 | if (!resp || resp.left(1).lower() == "n") |
| 3143 | return MYTH_SCHEMA_EXIT; |
| 3144 | |
| 3145 | return MYTH_SCHEMA_UPGRADE; |
| 3146 | } |
| 3147 | |