void show_Click(object sender, EventArgs e)
        {
            IPhoneBackup backup      = SelectedBackup;
            IPhoneApp    app         = backup.GetApps().FirstOrDefault(t => t.Name == "System");
            IPhoneFile   dbFile      = app.Files.FirstOrDefault(t => t.Path.Contains("sms.db"));
            MMSAnalyzer  mmsAnalyzer = new MMSAnalyzer(backup, dbFile);

            mmsAnalyzer.Show();
        }
        void hashes_Click(object sender, EventArgs e)
        {
            hashes.Enabled = false;
            IPhoneBackup backup = SelectedBackup;

            if (backup == null)
            {
                return;
            }
            IEnumerable <IPhoneApp> apps = backup.GetApps();

            if (apps == null)
            {
                return;
            }

            IEnumerable <IPhoneFile> files = apps.SelectMany(x => x.Files != null ? x.Files : new List <IPhoneFile>());

            StringBuilder builder = new StringBuilder();

            builder.AppendLine("Row,DateTime,MD5,SHA1,DisplayName,FileName");
            int row = 0;

            Model.InvokeAsync(files, delegate(IPhoneFile file) // run
            {
                FileInfo fileInfo = FileManager.GetOriginalFile(backup, file);
                string md5        = Util.MD5File(fileInfo);
                string sha1       = Util.SHA1File(fileInfo);

                builder.AppendLine(string.Format("{0},{1},{2},{3},{4},{5}",
                                                 row++,
                                                 DateTime.Now.ToUniversalTime(),
                                                 md5,
                                                 sha1,
                                                 file.Path,
                                                 fileInfo.FullName));
            }, delegate() // Called when completed
            {
                SaveFileDialog fileSaver = new SaveFileDialog();
                fileSaver.AddExtension   = true;
                fileSaver.DefaultExt     = ".csv";
                fileSaver.Filter         = "Comma separated file|*.csv";

                if (fileSaver.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(fileSaver.FileName, builder.ToString());
                }

                hashes.Enabled = true;
            }, "Analyzing hashes");
        }
        void start_Click(object sender, EventArgs e)
        {
            IPhoneBackup backup = SelectedBackup;

            if (backup == null)
            {
                return;
            }

            IPhoneApp  app      = backup.GetApps().FirstOrDefault(t => t.Name == "System");
            IPhoneFile dbFile   = app.Files.FirstOrDefault(t => t.Path.Contains("sms.db"));
            FileInfo   fileInfo = FileManager.GetWorkingFile(backup, dbFile, true);

            SMSAnalyzer analyzer = new SMSAnalyzer(backup, fileInfo.FullName);

            analyzer.Show();
        }
        public void SelectBackup(IPhoneBackup backup)
        {
            folderList.Items.Clear();
            fileList.Items.Clear();

            OnSelectedBackup(backup);

            UpdateTitle(backup.DisplayName);

            FileManager      fm   = FileManager.Current;
            List <IPhoneApp> apps = backup.GetApps();

            foreach (var app in apps)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Tag  = app;
                lvi.Text = app.Name;
                lvi.SubItems.Add(app.Files != null ? app.Files.Count.ToString() : "N/A");
                folderList.Items.Add(lvi);
            }
        }
        private void lstMms_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem.ListViewSubItem itm = e.Item.SubItems[3];
            if (itm != null)
            {
                if (!String.IsNullOrWhiteSpace(itm.Text))
                {
                    string     rowId = itm.Tag.ToString();
                    string     file  = itm.Text;
                    IPhoneFile iFile = _backup.GetApps()
                                       .FirstOrDefault(x => x.Name == "System")
                                       .Files
                                       .FirstOrDefault(y => y.Path.Contains(file) || y.Path.Contains(rowId));
                    Tuple <string, FileInfo> tuple = null;
                    if (iFile != null)
                    {
                        tuple = FileManager.Current.GetRandomFile(_backup, iFile);
                    }

                    if (tuple != null && (tuple.Item1.ToLower().Contains(".jpg") || tuple.Item1.ToLower().Contains(".jpeg")))
                    {
                        FileInfo info  = tuple.Item2;
                        Bitmap   image = new Bitmap(info.FullName);
                        picContainer.Tag   = info;
                        picContainer.Image = image;
                    }
                    else
                    {
                        picContainer.Image = Properties.Resources.help;
                        picContainer.Tag   = null;
                    }
                }
                else
                {
                    picContainer.Image = Properties.Resources.help;
                    picContainer.Tag   = null;
                }
            }
        }
        void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            IPhoneBackup     backup = e.Argument as IPhoneBackup;

            IPhoneApp system = backup.GetApps().FirstOrDefault(app => app.Name == "System");
            IEnumerable <IPhoneFile> images = system.Files.Where(file => file.Domain == "MediaDomain" && file.Path.Contains("DCIM/100APPLE"));
            FileManager fm = FileManager.Current;

            ImageList imgList = new ImageList();

            imgList.ImageSize  = new Size(64, 64);
            imgList.ColorDepth = ColorDepth.Depth32Bit;

            List <ListViewItem> listViewItems = new List <ListViewItem>();
            int length = images.Count(), current = 0;

            foreach (IPhoneFile file in images)
            {
                if (worker.CancellationPending)
                {
                    return;
                }
                FileInfo     info = fm.GetWorkingFileCurrentClass(_backup, file);
                ListViewItem itm  = new ListViewItem();
                itm.Tag      = new Bitmap(info.FullName);
                itm.Text     = info.Name;
                itm.ImageKey = info.Name;

                imgList.Images.Add(info.Name, (Bitmap)itm.Tag);
                listViewItems.Add(itm);

                worker.ReportProgress(Util.Percent(current++, length));
            }

            e.Result = new { ImageList = imgList, ListViewItems = listViewItems.ToArray() };
        }
        void _analyze_Click(object sender, EventArgs e)
        {
            IPhoneBackup             backup = SelectedBackup;
            IPhoneApp                app    = backup.GetApps().FirstOrDefault(t => t.Name == "System");
            IPhoneFile               dbFile = app.Files.FirstOrDefault(t => t.Path.Contains("consolidated"));
            IEnumerable <IPhoneFile> imgs   = app.Files.Where(t => t.Path.Contains("DCIM/100APPLE") && t.Domain == "MediaDomain");
            FileInfo fileInfo = FileManager.GetWorkingFile(backup, dbFile);

            Model.InvokeAsync(delegate(object w, DoWorkEventArgs a)
            {
                BackgroundWorker worker = w as BackgroundWorker;
                if (!worker.CancellationPending)
                {
                    List <Location> locations = new List <Location>();
                    using (SQLiteConnection con = new SQLiteConnection(@"Data Source=" + fileInfo.FullName))
                    {
                        con.Open();
                        var cmd         = new SQLiteCommand();
                        cmd.Connection  = con;
                        cmd.CommandText = "SELECT * FROM WifiLocation;";

                        var reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            locations.Add(new Location
                            {
                                Name      = "[Wifi] MAC: " + reader.GetString(0),
                                Time      = new DateTime(2001, 1, 1, 0, 0, 0).AddSeconds(reader.GetDouble(1)),
                                Latitude  = reader.GetDouble(2),
                                Longitude = reader.GetDouble(3)
                            });
                        }
                        reader.Close();

                        worker.ReportProgress(33);

                        cmd.CommandText = "SELECT * FROM CellLocation";
                        reader          = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            locations.Add(new Location
                            {
                                Name = "[Cell] MCC: " + reader.GetInt32(0) + " MNC: " +
                                       reader.GetInt32(1) + " LAC: " +
                                       reader.GetInt32(2) + " CELL ID:" +
                                       reader.GetInt32(3),
                                Time      = new DateTime(2001, 1, 1, 0, 0, 0).AddSeconds(reader.GetDouble(4)),
                                Latitude  = reader.GetDouble(5),
                                Longitude = reader.GetDouble(6)
                            });
                        }

                        worker.ReportProgress(33);
                    }

                    //Gives incorrect results.. FIXIT
                    //int count = imgs.Count() + 66, current = 66;
                    //foreach (IPhoneFile file in imgs)
                    //{
                    //    FileInfo info = FileManager.GetWorkingFile(backup, file);
                    //    ExifTagCollection er = new ExifTagCollection(info.FullName);

                    //    //not working...
                    //    Location location = new Location();
                    //    double lat = 0, lon = 0;
                    //    foreach (ExifTag tag in er)
                    //    {
                    //        if (tag.FieldName == "GPSLatitude")
                    //        {
                    //            string dec = Regex.Match(tag.Value, @"\d+\.\d+").ToString();
                    //            double.TryParse(dec, out lat);
                    //        }
                    //        else if (tag.FieldName == "GPSLongitude")
                    //        {
                    //            string dec = Regex.Match(tag.Value, @"\d+\.\d+").ToString();
                    //            double.TryParse(dec, out lon);
                    //        }
                    //    }
                    //    location.Latitude = lat;
                    //    location.Longitude = lon;
                    //    location.Name = "[Image] " + info.Name;
                    //    locations.Add(location);

                    //    worker.ReportProgress(Util.Percent(current++, count));
                    //}

                    geKML kml = Location.ToKML(locations.OrderBy(x => x.Time));
                    a.Result  = kml;
                }
                else
                {
                    a.Cancel = true;
                    return;
                }
            },
                              delegate(object w, RunWorkerCompletedEventArgs a)
            {
                geKML kml = a.Result as geKML;

                FileDialog dialog = new SaveFileDialog();
                dialog.DefaultExt = "kmz";
                dialog.FileName   = "locations.kmz";
                dialog.Filter     = "Google earth|*.kmz";
                if (dialog.ShowDialog(Model.Window) == DialogResult.OK)
                {
                    using (FileStream s = new FileStream(dialog.FileName, FileMode.Create))
                    {
                        using (BinaryWriter wr = new BinaryWriter(s))
                        {
                            wr.Write(kml.ToKMZ());
                        }
                    }
                }
            }, "Analyzing location", false);
        }