Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba0b79de64 | |||
| 60bd3163a9 | |||
| a3a3ac1dcc | |||
| 9d58a1a113 | |||
| bf0be12a5e | |||
| 59d4c094a9 | |||
| 499763d81f | |||
| 9aa6ccb224 | |||
| 235960f450 | |||
| 9298c3c287 |
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
; Erfordert Inno Setup 6.x (https://jrsoftware.org/isinfo.php)
|
; Erfordert Inno Setup 6.x (https://jrsoftware.org/isinfo.php)
|
||||||
|
|
||||||
#define MyAppName "Starface Outlook Sync"
|
#define MyAppName "Starface Outlook Sync"
|
||||||
#define MyAppVersion "0.0.0.4"
|
#define MyAppVersion "0.0.0.9"
|
||||||
#define MyAppPublisher "HackerSoft - Hacker-Net Telekommunikation"
|
#define MyAppPublisher "HackerSoft - Hacker-Net Telekommunikation"
|
||||||
#define MyAppURL "https://www.hacker-net.de"
|
#define MyAppURL "https://www.hacker-net.de"
|
||||||
#define MyAppExeName "StarfaceOutlookSync.exe"
|
#define MyAppExeName "StarfaceOutlookSync.exe"
|
||||||
|
|||||||
@@ -2,44 +2,75 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using StarfaceOutlookSync.Models;
|
using StarfaceOutlookSync.Models;
|
||||||
using Outlook = Microsoft.Office.Interop.Outlook;
|
|
||||||
|
|
||||||
namespace StarfaceOutlookSync.Services
|
namespace StarfaceOutlookSync.Services
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Zugriff auf Outlook-Kontakte per dynamic COM.
|
||||||
|
/// Funktioniert mit jeder Outlook Classic Version (2013-2024)
|
||||||
|
/// ohne Abhaengigkeit von einer bestimmten Interop-DLL.
|
||||||
|
/// </summary>
|
||||||
public class OutlookContactsService : IDisposable
|
public class OutlookContactsService : IDisposable
|
||||||
{
|
{
|
||||||
private Outlook.Application _outlookApp;
|
private dynamic _outlookApp;
|
||||||
private bool _weStartedOutlook;
|
private bool _weStartedOutlook;
|
||||||
|
|
||||||
// Marshal.GetActiveObject existiert nicht in .NET 8, daher P/Invoke
|
// OlDefaultFolders.olFolderContacts = 10
|
||||||
|
private const int OlFolderContacts = 10;
|
||||||
|
// OlItemType.olContactItem = 2
|
||||||
|
private const int OlContactItem = 2;
|
||||||
|
|
||||||
[DllImport("oleaut32.dll", PreserveSig = false)]
|
[DllImport("oleaut32.dll", PreserveSig = false)]
|
||||||
private static extern void GetActiveObject([MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, IntPtr pvReserved, [MarshalAs(UnmanagedType.IUnknown)] out object ppunk);
|
private static extern void GetActiveObject(
|
||||||
|
[MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
|
||||||
|
IntPtr pvReserved,
|
||||||
|
[MarshalAs(UnmanagedType.IUnknown)] out object ppunk);
|
||||||
|
|
||||||
private static object GetActiveComObject(string progId)
|
private static object GetActiveComObject(string progId)
|
||||||
{
|
{
|
||||||
var clsid = Type.GetTypeFromProgID(progId, true).GUID;
|
var type = Type.GetTypeFromProgID(progId, false);
|
||||||
GetActiveObject(clsid, IntPtr.Zero, out var obj);
|
if (type == null) return null;
|
||||||
|
GetActiveObject(type.GUID, IntPtr.Zero, out var obj);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Outlook.Application GetOutlookApp()
|
private dynamic GetOutlookApp()
|
||||||
{
|
{
|
||||||
if (_outlookApp != null) return _outlookApp;
|
if (_outlookApp != null) return _outlookApp;
|
||||||
|
|
||||||
|
// Versuch 1: Laufende Outlook-Instanz finden
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Versuche laufende Outlook-Instanz zu finden
|
var obj = GetActiveComObject("Outlook.Application");
|
||||||
_outlookApp = (Outlook.Application)GetActiveComObject("Outlook.Application");
|
if (obj != null)
|
||||||
_weStartedOutlook = false;
|
{
|
||||||
}
|
_outlookApp = obj;
|
||||||
catch
|
_weStartedOutlook = false;
|
||||||
{
|
return _outlookApp;
|
||||||
// Outlook starten falls nicht laufend
|
}
|
||||||
_outlookApp = new Outlook.Application();
|
|
||||||
_weStartedOutlook = true;
|
|
||||||
}
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
return _outlookApp;
|
// Versuch 2: Outlook per COM starten
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var outlookType = Type.GetTypeFromProgID("Outlook.Application", false);
|
||||||
|
if (outlookType != null)
|
||||||
|
{
|
||||||
|
_outlookApp = Activator.CreateInstance(outlookType);
|
||||||
|
_weStartedOutlook = true;
|
||||||
|
return _outlookApp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
"Outlook Classic konnte nicht gefunden werden.\n\n" +
|
||||||
|
"Moegliche Ursachen:\n" +
|
||||||
|
"- Outlook Classic ist nicht installiert\n" +
|
||||||
|
"- Outlook Classic ist nicht gestartet\n" +
|
||||||
|
"- Das neue Outlook wird verwendet (wird noch nicht unterstuetzt)\n\n" +
|
||||||
|
"Bitte Outlook Classic starten und erneut versuchen.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<string> GetContactFolderPaths()
|
public List<string> GetContactFolderPaths()
|
||||||
@@ -50,128 +81,235 @@ namespace StarfaceOutlookSync.Services
|
|||||||
var app = GetOutlookApp();
|
var app = GetOutlookApp();
|
||||||
var ns = app.GetNamespace("MAPI");
|
var ns = app.GetNamespace("MAPI");
|
||||||
|
|
||||||
// Standard-Kontaktordner
|
// Alle Stores durchgehen
|
||||||
var defaultFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
var stores = ns.Stores;
|
||||||
folders.Add(defaultFolder.FolderPath);
|
for (int i = 1; i <= (int)stores.Count; i++)
|
||||||
|
|
||||||
// Unterordner
|
|
||||||
AddSubFolders(defaultFolder, folders);
|
|
||||||
|
|
||||||
// Weitere Kontaktordner in anderen Stores
|
|
||||||
foreach (Outlook.Store store in ns.Stores)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var store = stores[i];
|
||||||
var rootFolder = store.GetRootFolder();
|
var rootFolder = store.GetRootFolder();
|
||||||
FindContactFolders(rootFolder, folders);
|
FindContactFoldersRecursive(rootFolder, folders);
|
||||||
|
Marshal.ReleaseComObject(rootFolder);
|
||||||
|
Marshal.ReleaseComObject(store);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Error scanning store {i}: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Falls nichts gefunden, Standard-Kontaktordner als Fallback
|
||||||
|
if (folders.Count == 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var defaultFolder = ns.GetDefaultFolder(OlFolderContacts);
|
||||||
|
folders.Add((string)defaultFolder.FolderPath);
|
||||||
|
Marshal.ReleaseComObject(defaultFolder);
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Marshal.ReleaseComObject(stores);
|
||||||
Marshal.ReleaseComObject(ns);
|
Marshal.ReleaseComObject(ns);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
System.Diagnostics.Debug.WriteLine($"Error getting folders: {ex.Message}");
|
System.Diagnostics.Debug.WriteLine($"Error getting folders: {ex.Message}");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
return folders;
|
return folders;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddSubFolders(Outlook.MAPIFolder folder, List<string> paths)
|
private void FindContactFoldersRecursive(dynamic folder, List<string> paths)
|
||||||
{
|
{
|
||||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
try
|
||||||
{
|
{
|
||||||
if (sub.DefaultItemType == Outlook.OlItemType.olContactItem)
|
if ((int)folder.DefaultItemType == OlContactItem)
|
||||||
{
|
{
|
||||||
if (!paths.Contains(sub.FolderPath))
|
string path = folder.FolderPath;
|
||||||
paths.Add(sub.FolderPath);
|
if (!paths.Contains(path))
|
||||||
AddSubFolders(sub, paths);
|
paths.Add(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var subFolders = folder.Folders;
|
||||||
|
for (int i = 1; i <= (int)subFolders.Count; i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var sub = subFolders[i];
|
||||||
|
FindContactFoldersRecursive(sub, paths);
|
||||||
|
Marshal.ReleaseComObject(sub);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(subFolders);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Error scanning folder: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FindContactFolders(Outlook.MAPIFolder folder, List<string> paths)
|
private dynamic FindFolderByPath(dynamic folder, string targetPath)
|
||||||
{
|
{
|
||||||
if (folder.DefaultItemType == Outlook.OlItemType.olContactItem)
|
try
|
||||||
{
|
{
|
||||||
if (!paths.Contains(folder.FolderPath))
|
string currentPath = folder.FolderPath;
|
||||||
paths.Add(folder.FolderPath);
|
if (currentPath == targetPath)
|
||||||
}
|
return folder;
|
||||||
|
|
||||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
var subs = folder.Folders;
|
||||||
{
|
for (int i = 1; i <= (int)subs.Count; i++)
|
||||||
FindContactFolders(sub, paths);
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var sub = subs[i];
|
||||||
|
var match = FindFolderByPath(sub, targetPath);
|
||||||
|
if (match != null) return match;
|
||||||
|
Marshal.ReleaseComObject(sub);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(subs);
|
||||||
}
|
}
|
||||||
|
catch { }
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Outlook.MAPIFolder GetFolderByPath(string folderPath)
|
private dynamic GetFolderByPath(string folderPath)
|
||||||
{
|
{
|
||||||
var app = GetOutlookApp();
|
var app = GetOutlookApp();
|
||||||
var ns = app.GetNamespace("MAPI");
|
var ns = app.GetNamespace("MAPI");
|
||||||
|
|
||||||
// Standard-Kontaktordner als Fallback
|
|
||||||
if (string.IsNullOrEmpty(folderPath))
|
if (string.IsNullOrEmpty(folderPath))
|
||||||
return ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
return ns.GetDefaultFolder(OlFolderContacts);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Pfad durchlaufen
|
// Versuch 1: Alle Kontaktordner durchsuchen und per FolderPath matchen
|
||||||
var parts = folderPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
|
var stores = ns.Stores;
|
||||||
Outlook.MAPIFolder current = null;
|
for (int i = 1; i <= (int)stores.Count; i++)
|
||||||
|
|
||||||
foreach (Outlook.Store store in ns.Stores)
|
|
||||||
{
|
{
|
||||||
if (store.GetRootFolder().Name == parts[0] ||
|
try
|
||||||
store.GetRootFolder().FolderPath.TrimStart('\\') == parts[0])
|
|
||||||
{
|
{
|
||||||
current = store.GetRootFolder();
|
var store = stores[i];
|
||||||
break;
|
var root = store.GetRootFolder();
|
||||||
}
|
var match = FindFolderByPath(root, folderPath);
|
||||||
}
|
if (match != null)
|
||||||
|
|
||||||
if (current == null)
|
|
||||||
return ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
|
||||||
|
|
||||||
for (int i = 1; i < parts.Length; i++)
|
|
||||||
{
|
|
||||||
bool found = false;
|
|
||||||
foreach (Outlook.MAPIFolder sub in current.Folders)
|
|
||||||
{
|
|
||||||
if (sub.Name == parts[i])
|
|
||||||
{
|
{
|
||||||
current = sub;
|
Marshal.ReleaseComObject(stores);
|
||||||
found = true;
|
return match;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
Marshal.ReleaseComObject(root);
|
||||||
|
Marshal.ReleaseComObject(store);
|
||||||
}
|
}
|
||||||
if (!found)
|
catch { }
|
||||||
return ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
}
|
||||||
|
Marshal.ReleaseComObject(stores);
|
||||||
|
|
||||||
|
// Versuch 2: Namespace.Folders direkt navigieren
|
||||||
|
var parts = folderPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (parts.Length >= 1)
|
||||||
|
{
|
||||||
|
var topFolders = ns.Folders;
|
||||||
|
for (int i = 1; i <= (int)topFolders.Count; i++)
|
||||||
|
{
|
||||||
|
var topFolder = topFolders[i];
|
||||||
|
if ((string)topFolder.Name == parts[0])
|
||||||
|
{
|
||||||
|
dynamic current = topFolder;
|
||||||
|
for (int p = 1; p < parts.Length; p++)
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
var subs = current.Folders;
|
||||||
|
for (int j = 1; j <= (int)subs.Count; j++)
|
||||||
|
{
|
||||||
|
var sub = subs[j];
|
||||||
|
if ((string)sub.Name == parts[p])
|
||||||
|
{
|
||||||
|
current = sub;
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(sub);
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(subs);
|
||||||
|
if (!found) { current = null; break; }
|
||||||
|
}
|
||||||
|
if (current != null)
|
||||||
|
{
|
||||||
|
Marshal.ReleaseComObject(topFolders);
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(topFolder);
|
||||||
|
}
|
||||||
|
Marshal.ReleaseComObject(topFolders);
|
||||||
}
|
}
|
||||||
|
|
||||||
return current;
|
System.Diagnostics.Debug.WriteLine($"Folder not found by path: {folderPath}, using default");
|
||||||
|
return ns.GetDefaultFolder(OlFolderContacts);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
return ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
return ns.GetDefaultFolder(OlFolderContacts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UnifiedContact> GetContacts(string folderPath)
|
public List<UnifiedContact> GetContacts(string folderPath)
|
||||||
{
|
{
|
||||||
var contacts = new List<UnifiedContact>();
|
var contacts = new List<UnifiedContact>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var folder = GetFolderByPath(folderPath);
|
var folder = GetFolderByPath(folderPath);
|
||||||
var items = folder.Items;
|
System.Diagnostics.Debug.WriteLine($"Reading contacts from: {(string)folder.FolderPath}");
|
||||||
|
|
||||||
foreach (var item in items)
|
var items = folder.Items;
|
||||||
|
int count = (int)items.Count;
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Items count: {count}");
|
||||||
|
|
||||||
|
for (int i = 1; i <= count; i++)
|
||||||
{
|
{
|
||||||
if (item is Outlook.ContactItem ci)
|
dynamic item = null;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
contacts.Add(MapFromOutlook(ci));
|
item = items[i];
|
||||||
Marshal.ReleaseComObject(ci);
|
int itemClass = -1;
|
||||||
|
try { itemClass = (int)item.Class; } catch { }
|
||||||
|
|
||||||
|
// olContact = 40, aber manche Kontakte melden Class anders
|
||||||
|
// Versuch einfach die Kontakt-Felder zu lesen
|
||||||
|
if (itemClass == 40)
|
||||||
|
{
|
||||||
|
contacts.Add(MapFromOutlook(item));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Fallback: Pruefen ob es trotzdem ein Kontakt ist
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string eid = item.EntryID;
|
||||||
|
string fn = item.FirstName;
|
||||||
|
string ln = item.LastName;
|
||||||
|
// Hat Kontakt-Felder -> ist ein Kontakt
|
||||||
|
contacts.Add(MapFromOutlook(item));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Kein Kontakt, ueberspringen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Error reading item {i}: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (item != null) try { Marshal.ReleaseComObject(item); } catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +329,8 @@ namespace StarfaceOutlookSync.Services
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var folder = GetFolderByPath(folderPath);
|
var folder = GetFolderByPath(folderPath);
|
||||||
var ci = (Outlook.ContactItem)folder.Items.Add(Outlook.OlItemType.olContactItem);
|
var items = folder.Items;
|
||||||
|
dynamic ci = items.Add(OlContactItem);
|
||||||
|
|
||||||
MapToOutlook(contact, ci);
|
MapToOutlook(contact, ci);
|
||||||
ci.Save();
|
ci.Save();
|
||||||
@@ -199,6 +338,7 @@ namespace StarfaceOutlookSync.Services
|
|||||||
contact.OutlookEntryId = ci.EntryID;
|
contact.OutlookEntryId = ci.EntryID;
|
||||||
|
|
||||||
Marshal.ReleaseComObject(ci);
|
Marshal.ReleaseComObject(ci);
|
||||||
|
Marshal.ReleaseComObject(items);
|
||||||
Marshal.ReleaseComObject(folder);
|
Marshal.ReleaseComObject(folder);
|
||||||
|
|
||||||
return contact;
|
return contact;
|
||||||
@@ -216,7 +356,7 @@ namespace StarfaceOutlookSync.Services
|
|||||||
{
|
{
|
||||||
var app = GetOutlookApp();
|
var app = GetOutlookApp();
|
||||||
var ns = app.GetNamespace("MAPI");
|
var ns = app.GetNamespace("MAPI");
|
||||||
var ci = (Outlook.ContactItem)ns.GetItemFromID(entryId);
|
dynamic ci = ns.GetItemFromID(entryId);
|
||||||
|
|
||||||
MapToOutlook(contact, ci);
|
MapToOutlook(contact, ci);
|
||||||
ci.Save();
|
ci.Save();
|
||||||
@@ -239,7 +379,7 @@ namespace StarfaceOutlookSync.Services
|
|||||||
{
|
{
|
||||||
var app = GetOutlookApp();
|
var app = GetOutlookApp();
|
||||||
var ns = app.GetNamespace("MAPI");
|
var ns = app.GetNamespace("MAPI");
|
||||||
var ci = (Outlook.ContactItem)ns.GetItemFromID(entryId);
|
dynamic ci = ns.GetItemFromID(entryId);
|
||||||
|
|
||||||
ci.Delete();
|
ci.Delete();
|
||||||
Marshal.ReleaseComObject(ci);
|
Marshal.ReleaseComObject(ci);
|
||||||
@@ -254,36 +394,46 @@ namespace StarfaceOutlookSync.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private UnifiedContact MapFromOutlook(Outlook.ContactItem ci)
|
private UnifiedContact MapFromOutlook(dynamic ci)
|
||||||
{
|
{
|
||||||
return new UnifiedContact
|
return new UnifiedContact
|
||||||
{
|
{
|
||||||
OutlookEntryId = ci.EntryID ?? "",
|
OutlookEntryId = (string)(ci.EntryID ?? ""),
|
||||||
FirstName = ci.FirstName ?? "",
|
FirstName = (string)(ci.FirstName ?? ""),
|
||||||
LastName = ci.LastName ?? "",
|
LastName = (string)(ci.LastName ?? ""),
|
||||||
Company = ci.CompanyName ?? "",
|
Company = (string)(ci.CompanyName ?? ""),
|
||||||
JobTitle = ci.JobTitle ?? "",
|
JobTitle = (string)(ci.JobTitle ?? ""),
|
||||||
Email = ci.Email1Address ?? "",
|
Email = (string)(ci.Email1Address ?? ""),
|
||||||
EmailSecondary = ci.Email2Address ?? "",
|
EmailSecondary = (string)(ci.Email2Address ?? ""),
|
||||||
PhoneWork = ci.BusinessTelephoneNumber ?? "",
|
PhoneWork = (string)(ci.BusinessTelephoneNumber ?? ""),
|
||||||
PhoneMobile = ci.MobileTelephoneNumber ?? "",
|
PhoneMobile = (string)(ci.MobileTelephoneNumber ?? ""),
|
||||||
PhoneHome = ci.HomeTelephoneNumber ?? "",
|
PhoneHome = (string)(ci.HomeTelephoneNumber ?? ""),
|
||||||
Fax = ci.BusinessFaxNumber ?? "",
|
Fax = (string)(ci.BusinessFaxNumber ?? ""),
|
||||||
Street = ci.BusinessAddressStreet ?? "",
|
Street = (string)(ci.BusinessAddressStreet ?? ""),
|
||||||
City = ci.BusinessAddressCity ?? "",
|
City = (string)(ci.BusinessAddressCity ?? ""),
|
||||||
PostalCode = ci.BusinessAddressPostalCode ?? "",
|
PostalCode = (string)(ci.BusinessAddressPostalCode ?? ""),
|
||||||
State = ci.BusinessAddressState ?? "",
|
State = (string)(ci.BusinessAddressState ?? ""),
|
||||||
Country = ci.BusinessAddressCountry ?? "",
|
Country = (string)(ci.BusinessAddressCountry ?? ""),
|
||||||
Website = ci.WebPage ?? "",
|
Website = (string)(ci.WebPage ?? ""),
|
||||||
Notes = ci.Body ?? "",
|
Notes = (string)(ci.Body ?? ""),
|
||||||
Salutation = ci.Title ?? "",
|
Salutation = (string)(ci.Title ?? ""),
|
||||||
Title = ci.Suffix ?? "",
|
Birthday = GetBirthdayString(ci)
|
||||||
Birthday = ci.Birthday != DateTime.MinValue && ci.Birthday.Year > 1900
|
|
||||||
? ci.Birthday.ToString("yyyy-MM-dd") : ""
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MapToOutlook(UnifiedContact contact, Outlook.ContactItem ci)
|
private string GetBirthdayString(dynamic ci)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DateTime bday = ci.Birthday;
|
||||||
|
if (bday.Year > 1900 && bday != DateTime.MinValue)
|
||||||
|
return bday.ToString("yyyy-MM-dd");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MapToOutlook(UnifiedContact contact, dynamic ci)
|
||||||
{
|
{
|
||||||
ci.FirstName = contact.FirstName;
|
ci.FirstName = contact.FirstName;
|
||||||
ci.LastName = contact.LastName;
|
ci.LastName = contact.LastName;
|
||||||
@@ -320,7 +470,7 @@ namespace StarfaceOutlookSync.Services
|
|||||||
{
|
{
|
||||||
try { _outlookApp.Quit(); } catch { }
|
try { _outlookApp.Quit(); } catch { }
|
||||||
}
|
}
|
||||||
Marshal.ReleaseComObject(_outlookApp);
|
try { Marshal.ReleaseComObject(_outlookApp); } catch { }
|
||||||
_outlookApp = null;
|
_outlookApp = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,11 +155,14 @@ namespace StarfaceOutlookSync.Services
|
|||||||
return books;
|
return books;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public event Action<string> OnDebug;
|
||||||
|
|
||||||
public async Task<List<UnifiedContact>> GetContactsAsync(StarfaceAddressBook book)
|
public async Task<List<UnifiedContact>> GetContactsAsync(StarfaceAddressBook book)
|
||||||
{
|
{
|
||||||
var contacts = new List<UnifiedContact>();
|
var contacts = new List<UnifiedContact>();
|
||||||
int page = 0;
|
int page = 0;
|
||||||
const int pageSize = 200;
|
const int pageSize = 200;
|
||||||
|
bool firstPage = true;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@@ -172,9 +175,47 @@ namespace StarfaceOutlookSync.Services
|
|||||||
var resp = await _http.GetAsync($"{_baseUrl}/contacts?{query}");
|
var resp = await _http.GetAsync($"{_baseUrl}/contacts?{query}");
|
||||||
if (!resp.IsSuccessStatusCode) break;
|
if (!resp.IsSuccessStatusCode) break;
|
||||||
|
|
||||||
var array = JArray.Parse(await resp.Content.ReadAsStringAsync());
|
var body = await resp.Content.ReadAsStringAsync();
|
||||||
|
JArray array;
|
||||||
|
|
||||||
|
// Die API gibt je nach Version ein Array oder ein Objekt mit "items" zurueck
|
||||||
|
var token = JToken.Parse(body);
|
||||||
|
if (token is JArray directArray)
|
||||||
|
{
|
||||||
|
array = directArray;
|
||||||
|
}
|
||||||
|
else if (token is JObject obj)
|
||||||
|
{
|
||||||
|
// Versuche gaengige Felder: items, contacts, data, results
|
||||||
|
array = (obj["items"] ?? obj["contacts"] ?? obj["data"] ?? obj["results"]) as JArray;
|
||||||
|
if (array == null)
|
||||||
|
{
|
||||||
|
// Einzelnes Kontakt-Objekt? Dann in Array wrappen
|
||||||
|
if (obj["id"] != null && obj["blocks"] != null)
|
||||||
|
{
|
||||||
|
array = new JArray { obj };
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Unerwartete Starface-Antwort: {body.Substring(0, Math.Min(200, body.Length))}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (array.Count == 0) break;
|
if (array.Count == 0) break;
|
||||||
|
|
||||||
|
// Ersten Kontakt als Debug-Info loggen
|
||||||
|
if (firstPage && array.Count > 0)
|
||||||
|
{
|
||||||
|
OnDebug?.Invoke($"Starface API Rohdaten (1. Kontakt):\n{array[0].ToString(Formatting.Indented)}");
|
||||||
|
firstPage = false;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var item in array)
|
foreach (var item in array)
|
||||||
contacts.Add(MapFromStarface(item));
|
contacts.Add(MapFromStarface(item));
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ namespace StarfaceOutlookSync.Services
|
|||||||
Log("Verbinde mit Starface...");
|
Log("Verbinde mit Starface...");
|
||||||
using (var starface = new StarfaceApiClient(profile.StarfaceConnection))
|
using (var starface = new StarfaceApiClient(profile.StarfaceConnection))
|
||||||
{
|
{
|
||||||
|
starface.OnDebug += (msg) => Log(msg);
|
||||||
var loginOk = await starface.LoginAsync();
|
var loginOk = await starface.LoginAsync();
|
||||||
if (!loginOk)
|
if (!loginOk)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,9 +7,9 @@
|
|||||||
<AssemblyTitle>Starface Outlook Sync</AssemblyTitle>
|
<AssemblyTitle>Starface Outlook Sync</AssemblyTitle>
|
||||||
<Company>HackerSoft - Hacker-Net Telekommunikation</Company>
|
<Company>HackerSoft - Hacker-Net Telekommunikation</Company>
|
||||||
<Product>Starface Outlook Sync</Product>
|
<Product>Starface Outlook Sync</Product>
|
||||||
<Version>0.0.0.4</Version>
|
<Version>0.0.0.9</Version>
|
||||||
<AssemblyVersion>0.0.0.4</AssemblyVersion>
|
<AssemblyVersion>0.0.0.9</AssemblyVersion>
|
||||||
<FileVersion>0.0.0.4</FileVersion>
|
<FileVersion>0.0.0.9</FileVersion>
|
||||||
<Description>Synchronisiert Outlook-Kontakte mit Starface Telefonanlage</Description>
|
<Description>Synchronisiert Outlook-Kontakte mit Starface Telefonanlage</Description>
|
||||||
<Copyright>Stefan Hacker - HackerSoft</Copyright>
|
<Copyright>Stefan Hacker - HackerSoft</Copyright>
|
||||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||||
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="Microsoft.Office.Interop.Outlook" Version="15.0.4797.1004" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace StarfaceOutlookSync.UI
|
|||||||
|
|
||||||
var lblVersion = new Label
|
var lblVersion = new Label
|
||||||
{
|
{
|
||||||
Text = "Version 0.0.0.4",
|
Text = "Version 0.0.0.9",
|
||||||
Left = 0, Top = 56, Width = 340, Height = 20,
|
Left = 0, Top = 56, Width = 340, Height = 20,
|
||||||
TextAlign = ContentAlignment.MiddleCenter,
|
TextAlign = ContentAlignment.MiddleCenter,
|
||||||
ForeColor = Color.Gray
|
ForeColor = Color.Gray
|
||||||
|
|||||||
@@ -142,9 +142,12 @@ namespace StarfaceOutlookSync.UI
|
|||||||
_outlookFolderPaths = outlook.GetContactFolderPaths();
|
_outlookFolderPaths = outlook.GetContactFolderPaths();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_outlookFolderPaths = new List<string> { "\\\\Kontakte" };
|
MessageBox.Show(
|
||||||
|
$"Outlook-Kontaktordner konnten nicht geladen werden:\n{ex.Message}\n\nIst Outlook gestartet?",
|
||||||
|
"Outlook-Verbindung", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
_outlookFolderPaths = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bestehende Werte laden
|
// Bestehende Werte laden
|
||||||
|
|||||||
Reference in New Issue
Block a user