History.xaml.cs 4.21 KB
Newer Older
Sylvain Berfini's avatar
Sylvain Berfini committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Linphone.Model;
using Linphone.Resources;

namespace Linphone.Views
{
15 16 17
    /// <summary>
    /// Page displaying the call logs.
    /// </summary>
18
    public partial class History : BasePage
Sylvain Berfini's avatar
Sylvain Berfini committed
19 20 21 22
    {
        private bool _usingSelectionAppBar = false;
        private IEnumerable<CallLog> _selection;

23 24 25
        /// <summary>
        /// Public constructor.
        /// </summary>
Sylvain Berfini's avatar
Sylvain Berfini committed
26 27 28 29 30 31
        public History()
        {
            InitializeComponent();
            BuildLocalizedApplicationBar();
        }

32 33 34 35 36
        /// <summary>
        /// Method called when the page is displayed.
        /// Fetches the logs from the LinphoneManager and displays them.
        /// </summary>
        /// <param name="e"></param>
Sylvain Berfini's avatar
Sylvain Berfini committed
37 38
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
39 40 41
            base.OnNavigatedTo(e);
            StatusBar = status;

42 43 44
            TileManager tileManager = TileManager.Instance;
            tileManager.RemoveMissedCallsTile();

Sylvain Berfini's avatar
Sylvain Berfini committed
45 46 47 48 49 50
            List<CallLogs> callsHistory = LinphoneManager.Instance.GetCallsHistory();
            history.ItemsSource = callsHistory;
        }

        private void deleteAll_Click_1(object sender, EventArgs e)
        {
51
            history.ItemsSource = LinphoneManager.Instance.ClearCallLogs();
Sylvain Berfini's avatar
Sylvain Berfini committed
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
        }

        private void deleteSelection_Click_1(object sender, EventArgs e)
        {
            history.ItemsSource = LinphoneManager.Instance.RemoveCallLogs(_selection);
        }

        private void BuildLocalizedApplicationBar()
        {
            ApplicationBar = new ApplicationBar();
            SetupAppBarForEmptySelection();
        }

        private void ClearApplicationBar()
        {
            while (ApplicationBar.Buttons.Count > 0)
            {
                ApplicationBar.Buttons.RemoveAt(0);
            }
        }

        private void SetupAppBarForEmptySelection()
        {
            ApplicationBarIconButton appBarDeleteAll = new ApplicationBarIconButton(new Uri("/Assets/AppBar/delete.png", UriKind.Relative));
            appBarDeleteAll.Text = AppResources.DeleteAllMenu;
            ApplicationBar.Buttons.Add(appBarDeleteAll);
            appBarDeleteAll.Click += deleteAll_Click_1;

            _usingSelectionAppBar = false;
        }

        private void SetupAppBarForSelectedItems()
        {
            ApplicationBarIconButton appBarDeleteSelection = new ApplicationBarIconButton(new Uri("/Assets/AppBar/delete.png", UriKind.Relative));
            appBarDeleteSelection.Text = AppResources.DeleteSelectionMenu;
            ApplicationBar.Buttons.Add(appBarDeleteSelection);
            appBarDeleteSelection.Click += deleteSelection_Click_1;

            _usingSelectionAppBar = true;
        }

        private void calls_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            LongListMultiSelector list = (LongListMultiSelector)sender;
            if (list.SelectedItems.Count == 0)
            {
                ClearApplicationBar();
                SetupAppBarForEmptySelection();
            }
            else if (list.SelectedItems.Count >= 1 && !_usingSelectionAppBar) // Do it only once, when selection was empty and isn't anymore
            {
                _selection = list.SelectedItems.Cast<CallLog>();
                ClearApplicationBar();
                SetupAppBarForSelectedItems();
            }
        }

        private void SetAddressGoToDialerAndCall(String address)
        {
            NavigationService.Navigate(new Uri("/Views/Dialer.xaml?sip=" + address, UriKind.RelativeOrAbsolute));
        }

        private void callLog_Click_1(object sender, RoutedEventArgs e)
        {
            CallLog log = ((sender as Button).Tag as CallLog);

            String address;
            if (log.IsIncoming)
                address = log.From;
            else
                address = log.To;

            SetAddressGoToDialerAndCall(address);
        }
    }
}