EmailAppNCenter.c 11.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
#include "MMI_features.h"

#ifdef __MMI_EMAIL__
#ifdef __MMI_NCENTER_SUPPORT__


#include "MMIDataType.h"
#include "GlobalResDef.h"
#include "mmi_rp_app_email_def.h"
#include "Wgui_categories_util.h"

#include "emailsrvgprot.h"
#include "vsrv_ncenter.h"

#include "EmailAppGProt.h"
#include "EmailAppNCenter.h"
#include "EmailAppNetwork.h"

#define EMAIL_NC_BUF_LEN 128
typedef struct
{
    EMAIL_ACCT_ID acc_id;
    vsrv_notification_handle ongoing_nhandle;
    vsrv_notification_handle err_handle;
    S32 err_code;
}email_nc_map_struct;

static WCHAR ctext[EMAIL_NC_BUF_LEN + 1];
static email_nc_map_struct g_nc_list[MMI_EMAIL_MAX_ACCTS];

static S32 g_email_nc_total;
static vsrv_ngroup_handle g_email_nc_ghandle;
static vsrv_notification_handle g_email_nc_newmail_nhandle;


static void mmi_email_ncenter_newmail_callback(vsrv_notification_handle nhandle, vsrv_nintent intent, void* userData, U32 userDataSize);
static void mmi_email_ncenter_error_callback(vsrv_notification_handle nhandle, vsrv_nintent intent, void* userData, U32 userDataSize);
static void mmi_email_ncenter_ongoing_callback(vsrv_notification_handle nhandle, vsrv_nintent intent, void* userData, U32 userDataSize);


static WCHAR* error_get_filename(void);
static void error_read(void);
static void error_write(void);
//static void error_restore(void);


static WCHAR *get_acct_name(EMAIL_ACCT_ID acc_id)
{
    srv_email_acct_get_name(
        acc_id, 
        ctext, 
        EMAIL_NC_BUF_LEN);
    ctext[EMAIL_NC_BUF_LEN] = 0;

    return ctext;
}


static void create_event_ngroup(void)
{
    g_email_nc_ghandle = vsrv_ncenter_create_ngroup(VSRV_NGROUP_TYPE_SINGLE_TITLE, APP_EMAIL);
    vsrv_ncenter_set_ngroup_title_id(g_email_nc_ghandle, STR_GLOBAL_EMAIL);
}

/*static vsrv_notification_handle get_err_handle(EMAIL_ACCT_ID acc_id)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id)
        {
            return g_nc_list[i].err_handle;
        }
    }

    return NULL;
}*/


/*static vsrv_notification_handle get_onging_handle(EMAIL_ACCT_ID acc_id)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id)
        {
            return g_nc_list[i].ongoing_nhandle;
        }
    }

    return NULL;
}*/

static void add_ongoing_handle(EMAIL_ACCT_ID acc_id, vsrv_notification_handle h)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id)
        {
            g_nc_list[i].ongoing_nhandle = h;
            return;
        }
    }

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == 0)
        {
            g_nc_list[i].acc_id = acc_id;
            g_nc_list[i].ongoing_nhandle = h;
            return;
        }
    }

    ASSERT(0);
}

static void add_err_handle(EMAIL_ACCT_ID acc_id, S32 err_code, vsrv_notification_handle h)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id)
        {
            g_nc_list[i].err_handle = h;
            g_nc_list[i].err_code = err_code;
            return;
        }
    }

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == 0)
        {
            g_nc_list[i].acc_id = acc_id;
            g_nc_list[i].err_handle = h;
            g_nc_list[i].err_code = err_code;
            return;
        }
    }

    ASSERT(0);
}


static void remove_ongoing_handle(EMAIL_ACCT_ID acc_id)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id)
        {
            vsrv_ncenter_close_notification(g_nc_list[i].ongoing_nhandle);
            g_nc_list[i].ongoing_nhandle = NULL;
            if (g_nc_list[i].err_handle == NULL)
            {
                g_nc_list[i].acc_id = 0;
            }
            return;
        }
    }
}


static void remove_err_handle(EMAIL_ACCT_ID acc_id)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id)
        {
            vsrv_ncenter_close_notification(g_nc_list[i].err_handle);
            g_nc_list[i].err_handle = NULL;
            g_nc_list[i].err_code = 0;

            if (g_nc_list[i].ongoing_nhandle == NULL)
            {
                g_nc_list[i].acc_id = 0;
            }

            error_write();
            return;
        }
    }


}


void mmi_email_ncenter_delaccount(EMAIL_ACCT_ID acc_id)
{
    // Clear all ONGOING ERROR 
    remove_err_handle(acc_id);
    remove_ongoing_handle(acc_id);
}

void mmi_email_ncenter_deinit(void)
{
    S32 i;

    vsrv_ncenter_close_notification(g_email_nc_newmail_nhandle);
    g_email_nc_newmail_nhandle = NULL;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id != 0)
        {
            if (g_nc_list[i].err_handle != NULL)
            {
                vsrv_ncenter_close_notification(g_nc_list[i].err_handle);
                g_nc_list[i].err_handle = NULL;
            }

            if (g_nc_list[i].ongoing_nhandle != NULL)
            {
                vsrv_ncenter_close_notification(g_nc_list[i].ongoing_nhandle);
                g_nc_list[i].ongoing_nhandle = NULL;
            }

            g_nc_list[i].acc_id = 0;
        }
    }
}


void mmi_email_ncenter_newmail(S32 total)
{
    g_email_nc_total = total;
    if (total == 0)
    {
        if (g_email_nc_newmail_nhandle != NULL)
        {
            vsrv_ncenter_close_notification(g_email_nc_newmail_nhandle);
            g_email_nc_newmail_nhandle = NULL;
        }
        return;
    }

    create_event_ngroup();

    if (g_email_nc_newmail_nhandle == NULL)
    {
        g_email_nc_newmail_nhandle = vsrv_ncenter_create_notification(VSRV_NOTIFICATION_TYPE_EVENT, g_email_nc_ghandle, 0);
        vsrv_ncenter_set_notification_intent_callback(g_email_nc_newmail_nhandle, mmi_email_ncenter_newmail_callback, NULL, 0);
    }

    kal_wsprintf(ctext, "%d ", g_email_nc_total);
    kal_wstrncat(ctext, (WCHAR*)GetString(STR_EMAIL_NCENTER_NEWMAIL), EMAIL_NC_BUF_LEN - kal_wstrlen(ctext));

    vsrv_ncenter_set_notification_maintext_str(g_email_nc_newmail_nhandle, ctext);
    vsrv_ncenter_notify_notification(g_email_nc_newmail_nhandle); 
}



static void mmi_email_ncenter_newmail_callback(vsrv_notification_handle nhandle, vsrv_nintent intent, void* userData, U32 userDataSize)
{
     if (intent.type == VSRV_NINTENT_TYPE_LAUNCH)
     {
         g_email_nc_newmail_nhandle = NULL;
         mmi_email_cmcc_new_email_hdlr();
     }
     else if (intent.type == VSRV_NINTENT_TYPE_CLEAR)
     {
         g_email_nc_newmail_nhandle = NULL;
         remove_all_received_number();
     }
     else if (intent.type == VSRV_NINTENT_TYPE_LANGUAGE_CHANGED)
     {
         if (g_email_nc_newmail_nhandle != NULL && g_email_nc_total > 0)
         {
             kal_wsprintf(ctext, "%d ", g_email_nc_total);
             kal_wstrncat(ctext, (WCHAR*)GetString(STR_EMAIL_NCENTER_NEWMAIL), EMAIL_NC_BUF_LEN - kal_wstrlen(ctext));

             vsrv_ncenter_set_notification_maintext_str(g_email_nc_newmail_nhandle, ctext);
             vsrv_ncenter_notify_notification(g_email_nc_newmail_nhandle); 
         }
     }
}




void mmi_email_ncenter_error(EMAIL_ACCT_ID acc_id, S32 error_code)
{
    vsrv_notification_handle nhandle;

    remove_err_handle(acc_id);
    create_event_ngroup();

    nhandle = vsrv_ncenter_create_notification(VSRV_NOTIFICATION_TYPE_EVENT, g_email_nc_ghandle, acc_id);
    add_err_handle(acc_id, error_code, nhandle);

    vsrv_ncenter_set_notification_maintext_str(nhandle, get_acct_name(acc_id));

    kal_wstrncpy(ctext, (WCHAR*)GetString(STR_EMAIL_NCENTER_ERROR), EMAIL_NC_BUF_LEN);
    kal_wstrncat(ctext, (WCHAR*)GetString(error_code), EMAIL_NC_BUF_LEN - kal_wstrlen(ctext));
    vsrv_ncenter_set_notification_subtext_str(nhandle, ctext);

    vsrv_ncenter_set_notification_intent_callback(nhandle, mmi_email_ncenter_error_callback, (void*)&acc_id, 4);
    vsrv_ncenter_notify_notification(nhandle); 

    //Write to FILESYSTEM
    error_write();
}

static void refresh_error(vsrv_notification_handle nhandle, EMAIL_ACCT_ID acc_id)
{
    S32 i;

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id == acc_id && g_nc_list[i].err_handle == nhandle)
        {
            kal_wstrncpy(ctext, (WCHAR*)GetString(STR_EMAIL_NCENTER_ERROR), EMAIL_NC_BUF_LEN);
            kal_wstrncat(ctext, (WCHAR*)GetString(g_nc_list[i].err_code), EMAIL_NC_BUF_LEN - kal_wstrlen(ctext));
            vsrv_ncenter_set_notification_subtext_str(nhandle, ctext);

            vsrv_ncenter_notify_notification(nhandle); 
            return;
        }
    }
}

static void mmi_email_ncenter_error_callback(vsrv_notification_handle nhandle, vsrv_nintent intent, void* userData, U32 userDataSize)
{
     if (intent.type == VSRV_NINTENT_TYPE_LAUNCH)
     {
         mmi_email_entry_main_screen_from_cosmos(NULL, 0);
         remove_err_handle((EMAIL_ACCT_ID)(*(EMAIL_ACCT_ID*)userData));
     }
     else if (intent.type == VSRV_NINTENT_TYPE_CLEAR)
     {
         remove_err_handle((EMAIL_ACCT_ID)(*(EMAIL_ACCT_ID*)userData));
     }
     else if (intent.type == VSRV_NINTENT_TYPE_LANGUAGE_CHANGED)
     {
         refresh_error(nhandle, (EMAIL_ACCT_ID)(*(EMAIL_ACCT_ID*)userData));
     }
}







void mmi_email_ncenter_ongoing_start(EMAIL_ACCT_ID acc_id, MMI_BOOL refreshing)
{
    vsrv_notification_handle nhandle;
    mmi_email_ncenter_ongoing_done(acc_id);
    remove_err_handle(acc_id);

    create_event_ngroup();

    nhandle = vsrv_ncenter_create_notification(VSRV_NOTIFICATION_TYPE_ONGOING, g_email_nc_ghandle, acc_id + 100);
    vsrv_ncenter_set_notification_autoclose(nhandle, FALSE, VSRV_NOTIFICATION_FLAG_AUTO_CLOSE_AFTER_TERMINATE);
    add_ongoing_handle(acc_id, nhandle);

    vsrv_ncenter_set_notification_maintext_id(nhandle, refreshing ? STR_EMAIL_NCENTER_REFRESHING : STR_EMAIL_NCENTER_SENDING);
    vsrv_ncenter_set_notification_subtext_str(nhandle, get_acct_name(acc_id));
    vsrv_ncenter_set_notification_questiontext_id(nhandle, refreshing ? STR_EMAIL_NCENTER_STOP_REFRESH : STR_EMAIL_NCENTER_STOP_SEND);
    vsrv_ncenter_set_notification_intent_callback(nhandle, mmi_email_ncenter_ongoing_callback, (void*)&acc_id, 4);
    vsrv_ncenter_notify_notification(nhandle);
}


void mmi_email_ncenter_ongoing_done(EMAIL_ACCT_ID acc_id)
{
    remove_ongoing_handle(acc_id);
}


static void mmi_email_ncenter_ongoing_callback(vsrv_notification_handle nhandle, vsrv_nintent intent, void* userData, U32 userDataSize)
{
    if (intent.type == VSRV_NINTENT_TYPE_LAUNCH)
    {
        mmi_email_entry_main_screen_from_cosmos(NULL, 0);
    }
    else if (intent.type == VSRV_NINTENT_TYPE_TERMINATE)
    {
        mmi_email_app_nwk_user_abort((EMAIL_ACCT_ID)(*(EMAIL_ACCT_ID*)userData));
    }
}



void mmi_email_ncenter_restore_error(void)
{
    S32 i;

    error_read();

    for (i = 0; i < MMI_EMAIL_MAX_ACCTS; i++)
    {
        if (g_nc_list[i].acc_id != 0 && g_nc_list[i].err_code != 0)
        {
            mmi_email_ncenter_error(g_nc_list[i].acc_id, g_nc_list[i].err_code);
        }
        else
        {
            memset(&g_nc_list[i], 0, sizeof(email_nc_map_struct));
        }
    }
}


static WCHAR* error_get_filename(void)
{
    static WCHAR filename[32];

    kal_wsprintf(filename, "%slasterrornoti", srv_email_get_root_path());

    return filename;
}

static void error_read(void)
{
    U32 readed = 0;
    FS_HANDLE h = 0;

    h = FS_Open(error_get_filename(), FS_READ_ONLY);
    if (h >= 0)
    {
        FS_Read(h, &g_nc_list, sizeof(g_nc_list), &readed);
        FS_Close(h);
    }

    if (readed != sizeof(g_nc_list))
    {
        memset(&g_nc_list, 0, sizeof(g_nc_list));
    }
}

static void error_write(void)
{
    FS_HANDLE h = 0;
    h = FS_Open(error_get_filename(), FS_READ_WRITE | FS_CREATE_ALWAYS);
    if (h < 0)
    {
        return;
    }

    FS_Write(h, &g_nc_list, sizeof(g_nc_list), NULL);
    FS_Close(h);
}



#endif /* __MMI_NCENTER_SUPPORT__ */
#endif /* __MMI_EMAIL__ */