Fixed can not install to sdcard
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / job_widget_uninstall.cpp
index 3cb8d82..4dc7745 100644 (file)
@@ -14,6 +14,8 @@
  *    limitations under the License.
  */
 
+#include <regex.h>
+#include <sys/stat.h>
 #include <widget_uninstall/job_widget_uninstall.h>
 #include <widget_uninstall/widget_uninstall_errors.h>
 #include <widget_uninstall/task_check.h>
 #include <widget_uninstall/task_smack.h>
 #include <widget_uninstall/task_uninstall_ospsvc.h>
 #include <widget_uninstall/task_delete_certificates.h>
+#include <dpl/wrt-dao-ro/global_config.h>
 #include <pkg-manager/pkgmgr_signal.h>
 #include <app2ext_interface.h>
 
 using namespace WrtDB;
 
 namespace { //anonymous
+const char* REG_TIZEN_PKGID_PATTERN = "^[a-zA-Z0-9]{10}$";
+const int PKGID_LENTH = 10;
+
+bool checkDirectoryExist(const std::string& pkgId)
+{
+    std::string installPath =
+        std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
+        "/" + pkgId;
+
+    struct stat dirStat;
+    if ((stat(installPath.c_str(), &dirStat) == 0)) {
+        return true;
+    }
+    return false;
+}
+
 class UninstallerTaskFail :
     public DPL::TaskDecl<UninstallerTaskFail>
 {
@@ -37,9 +56,9 @@ class UninstallerTaskFail :
 
     void StepFail()
     {
-        if(m_uninstalled) {
+        if (m_uninstalled) {
             ThrowMsg(Jobs::WidgetUninstall::Exceptions::WidgetNotExist,
-                    "Widget does not exist");
+                     "Widget does not exist");
         } else {
             Throw(Jobs::WidgetUninstall::Exceptions::Base);
         }
@@ -57,53 +76,104 @@ class UninstallerTaskFail :
 
 namespace Jobs {
 namespace WidgetUninstall {
-JobWidgetUninstall::JobWidgetUninstall(const std::string & widgetPkgName,
-        const WidgetUninstallationStruct &uninstallerStruct) :
+JobWidgetUninstall::JobWidgetUninstall(
+    const std::string & tizenAppId,
+    const WidgetUninstallationStruct &
+    uninstallerStruct) :
     Job(Uninstallation),
     JobContextBase<WidgetUninstallationStruct>(uninstallerStruct)
 {
     using namespace PackageManager;
     m_context.removeStarted = false;
     m_context.removeFinished = false;
+    m_context.removeAbnormal = false;
     m_context.uninstallStep = UninstallerContext::UNINSTALL_START;
     m_context.job = this;
-    m_context.pkgname = widgetPkgName;
-    m_context.isExternalWidget = getExternalWidgetFlag();
 
     Try
     {
-        WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(widgetPkgName));
-        m_context.locations = WidgetLocation(m_context.pkgname);
-
-        LogInfo("Widget model exists. Pkg name: " << m_context.pkgname);
-
-        AddTask(new TaskSmack(m_context));
-        AddTask(new TaskCheck(m_context));
+        WidgetStatus status = getWidgetStatus(tizenAppId);
+
+        if (WidgetStatus::Ok == status) {
+            WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(m_context.tzAppid));
+            m_context.tzPkgid = DPL::ToUTF8String(dao.getTizenPkgId());
+            m_context.locations = WidgetLocation(m_context.tzPkgid);
+            m_context.locations->registerAppid(m_context.tzAppid);
+
+            LogInfo("Widget model exists. App id : " << m_context.tzAppid);
+
+            AddTask(new TaskSmack(m_context));
+            AddTask(new TaskCheck(m_context));
+
+            if (dao.getPackagingType() == PKG_TYPE_HYBRID_WEB_APP) {
+                AddTask(new TaskUninstallOspsvc(m_context));
+            }
+            AddTask(new TaskRemoveFiles(m_context));
+            AddTask(new TaskDbUpdate(m_context));
+            AddTask(new TaskRemoveCustomHandlers(m_context));
+            AddTask(new TaskDeleteCertificates(m_context));
+
+            // send start signal of pkgmgr
+            if (getInstallerStruct().pkgmgrInterface->setPkgname(m_context.tzPkgid))
+            {
+                getInstallerStruct().pkgmgrInterface->sendSignal(
+                        PKGMGR_START_KEY,
+                        PKGMGR_START_UNINSTALL);
+            }
+        } else if (WidgetStatus::NOT_INSTALLED == status) {
+            AddTask(new UninstallerTaskFail(true));
+        } else if (WidgetStatus::ABNORMAL == status) {
+            m_context.locations = WidgetLocation(m_context.tzPkgid);
+            m_context.removeAbnormal = true;
+            AddTask(new TaskRemoveFiles(m_context));
+        } else {
+            AddTask(new UninstallerTaskFail(false));
+        }
+    } Catch(WidgetDAOReadOnly::Exception::Base) {
+        AddTask(new UninstallerTaskFail(false));
+    }
+}
 
-        if (dao.getPackagingType() == PKG_TYPE_HYBRID_WEB_APP) {
-            AddTask(new TaskUninstallOspsvc(m_context));
+JobWidgetUninstall::WidgetStatus JobWidgetUninstall::getWidgetStatus(const std::string &id)
+{
+    regex_t regx;
+    if(regcomp(&regx, REG_TIZEN_PKGID_PATTERN, REG_NOSUB | REG_EXTENDED)!=0){
+        LogDebug("Regcomp failed");
+    }
+    std::string pkgId;
+
+    Try {
+        if ((regexec(&regx, id.c_str(),
+                        static_cast<size_t>(0), NULL, 0) == REG_NOERROR)) {
+            pkgId = id;
+
+            TizenAppId appid =
+                WrtDB::WidgetDAOReadOnly::getTzAppId(
+                        DPL::FromUTF8String(id));
+            LogDebug("Get appid from pkgid : " << appid);
+            m_context.tzAppid = DPL::ToUTF8String(appid);
+        } else {
+            pkgId = id.substr(0, PKGID_LENTH);
+            WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(id));
+            m_context.tzAppid = id;
         }
-        AddTask(new TaskRemoveFiles(m_context));
-        AddTask(new TaskDbUpdate(m_context));
-        AddTask(new TaskRemoveCustomHandlers(m_context));
-        AddTask(new TaskDeleteCertificates(m_context));
-
-        // send start signal of pkgmgr
-        if (getInstallerStruct().pkgmgrInterface->setPkgname(m_context.pkgname)) {
-            getInstallerStruct().pkgmgrInterface->sendSignal(
-                    PKGMGR_START_KEY,
-                    PKGMGR_START_UNINSTALL);
+    } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
+        LogDebug("package id : " << pkgId);
+        m_context.tzPkgid = pkgId;
+        if (!pkgId.empty()) {
+            if(checkDirectoryExist(pkgId)) {
+                LogError("installed widget status is abnormal");
+                return WidgetStatus::ABNORMAL;
+            }
         }
-    } Catch (WidgetDAOReadOnly::Exception::WidgetNotExist) {
-        AddTask(new UninstallerTaskFail(true));
-    } Catch (WidgetDAOReadOnly::Exception::Base) {
-        AddTask(new UninstallerTaskFail(false));
+        return WidgetStatus::NOT_INSTALLED;
     }
+    return WidgetStatus::Ok;
 }
 
 std::string JobWidgetUninstall::getRemovedTizenId() const
 {
-    return m_context.pkgname;
+    return m_context.tzAppid;
 }
 
 bool JobWidgetUninstall::getRemoveStartedFlag() const
@@ -120,19 +190,17 @@ void JobWidgetUninstall::SendProgress()
 {
     using namespace PackageManager;
     if (!getRemoveStartedFlag() ||
-        (getRemoveStartedFlag() && getRemoveFinishedFlag())) {
+        (getRemoveStartedFlag() && getRemoveFinishedFlag()))
+    {
         if (NULL != getInstallerStruct().progressCallback) {
             // send progress signal of pkgmgr
             std::ostringstream percent;
             percent << static_cast<int>(GetProgressPercent());
-            getInstallerStruct().pkgmgrInterface->sendSignal(
-                        PKGMGR_PROGRESS_KEY,
-                        percent.str());
 
             LogDebug("Call widget uninstall progressCallback");
             getInstallerStruct().progressCallback(
-                    getInstallerStruct().userParam,
-                    GetProgressPercent(), GetProgressDescription());
+                getInstallerStruct().userParam,
+                GetProgressPercent(), GetProgressDescription());
         }
     }
 }
@@ -142,12 +210,13 @@ void JobWidgetUninstall::SendFinishedSuccess()
     using namespace PackageManager;
     // send signal of pkgmgr
     getInstallerStruct().pkgmgrInterface->sendSignal(
-                PKGMGR_END_KEY,
-                PKGMGR_END_SUCCESS);
+        PKGMGR_END_KEY,
+        PKGMGR_END_SUCCESS);
 
     LogDebug("Call widget uninstall success finishedCallback");
     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
-            getRemovedTizenId(),Exceptions::Success);
+                                          getRemovedTizenId(),
+                                          Jobs::Exceptions::Success);
 }
 
 void JobWidgetUninstall::SendFinishedFailure()
@@ -158,31 +227,20 @@ void JobWidgetUninstall::SendFinishedFailure()
 
     // send signal of pkgmgr
     getInstallerStruct().pkgmgrInterface->sendSignal(
-                PKGMGR_END_KEY,
-                PKGMGR_END_FAILURE);
+        PKGMGR_END_KEY,
+        PKGMGR_END_FAILURE);
 
     LogDebug("Call widget uninstall failure finishedCallback");
     getInstallerStruct().finishedCallback(getInstallerStruct().userParam,
-            getRemovedTizenId(), m_exceptionCaught); //TODO
+                                          getRemovedTizenId(),
+                                          m_exceptionCaught); //TODO
     LogDebug("[JobWidgetUninstall] Asynchronous failure callback status sent");
 }
 
 void JobWidgetUninstall::SaveExceptionData(const Jobs::JobExceptionBase &e)
 {
-    m_exceptionCaught = static_cast<Exceptions::Type>(e.getParam());
+    m_exceptionCaught = static_cast<Jobs::Exceptions::Type>(e.getParam());
     m_exceptionMessage = e.GetMessage();
 }
-
-bool JobWidgetUninstall::getExternalWidgetFlag() const
-{
-
-    LogDebug("Get external widget");
-    if (APP2EXT_SD_CARD == app2ext_get_app_location(m_context.pkgname.c_str())) {
-        LogDebug("This widget is in external stroage");
-        return true;
-    }
-    return false;
-}
-
 } //namespace WidgetUninstall
 } //namespace Jobs